App Designer provides a tutorial that guides you through the process of creating a simple app containing a plot and a slider. The slider controls the amplitude of the plotted function. You can create this app by running the tutorial, or you can follow the tutorial steps listed here.
To run the tutorial in App Designer, open the App Designer Start Page and expand the Examples: General section. Then, select Interactive Tutorial.
Perform the following steps in App Designer.
Drag an Axes component from the Component Library onto the canvas.
Drag a Slider component from the Component Library onto the canvas. Place it below the axes, as in the preceding image.
Replace the slider label text. Double-click the label and replace the word
Slider
with Amplitude
.
Above the canvas, click Code View to edit the code. (Notice that you can switch back to edit your layout by clicking Design View.)
In the code view, add a callback function that executes MATLAB® commands whenever the user moves the slider. Right-click
app.AmplitudeSlider
in the Component
Browser. Then select Callbacks > Add ValueChangedFcn callback in the context menu. App Designer creates a callback function
and places the cursor in the body of that function.
Plot the peaks
function in the
axes. Add this command to the second line of the
AmplitudeSliderValueChanged
callback:
plot(app.UIAxes,value*peaks)
plot
command specifies the target axes
(app.UIAxes
) as the first argument. The target axes
is always required when you call the plot
command in
App Designer.Change the limits of the y-axis by setting the
YLim
property of the UIAxes
object. Add this command to the third line of the
AmplitudeSliderValueChanged
callback:
app.UIAxes.YLim = [-1000 1000];
YLim
property. Always use the pattern app
.Component
.Property
to access property values.Click Run
to save and run the app. After saving
your changes, your app is available for running again in App Designer, or by
typing its name (without the
.mlapp
extension) at the
MATLAB command prompt. When you run the app from the command prompt,
the file must be in the current folder or on the MATLAB path.