Need help in dropdownopening callback.

12 views (last 30 days)
Aman kumar
Aman kumar on 5 Sep 2021
Edited: Srijith Kasaragod on 20 Sep 2021
I am designing an app for various signal display. (Attached UI and code).
1: Select a signal from dropdown. (how to write the callback for this)
2: Primary graph of that signal will be plotted on the axes. (how to write the callback for this)
3: Control the value of x axis and y axis from the slider. (how to write the callback for this)
classdef app1 < matlab.apps.AppBase
% Properties that corr
% Callbacks that handle component events
methods (Access = private)
% Value changed function: DropDown
function DropDownValueChanged(app, event)
x = 0:0.01: (360/180)*pi;
y = sin(x);
plot(app.UIAxes,y);
end
% Value changing function: Slider
function SliderValueChanging(app, event)
end
% Drop down opening function: DropDown
function DropDownOpening(app, event)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create Panel
app.Panel = uipanel(app.UIFigure);
app.Panel.Title = 'Panel';
app.Panel.Scrollable = 'on';
app.Panel.Position = [171 43 400 342];
% Create UIAxes
app.UIAxes = uiaxes(app.Panel);
title(app.UIAxes, 'sine signal')
xlabel(app.UIAxes, 'x')
ylabel(app.UIAxes, 'y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.XLim = [0 50];
app.UIAxes.YLim = [0 50];
app.UIAxes.XTick = [0 10 20 30 40 50];
app.UIAxes.XTickLabel = {'0'; '10'; '20'; '30'; '40'; '50'};
app.UIAxes.YTick = [0 25 50];
app.UIAxes.YTickLabel = {'0'; '25'; '50'};
app.UIAxes.XGrid = 'on';
app.UIAxes.YGrid = 'on';
app.UIAxes.Position = [42 14 325 100];
% Create SliderLabel
app.SliderLabel = uilabel(app.Panel);
app.SliderLabel.HorizontalAlignment = 'right';
app.SliderLabel.Position = [104 226 36 22];
app.SliderLabel.Text = 'Slider';
% Create Slider
app.Slider = uislider(app.Panel);
app.Slider.ValueChangingFcn = createCallbackFcn(app, @SliderValueChanging, true);
app.Slider.Position = [161 235 186 7];
% Create Slider2Label
app.Slider2Label = uilabel(app.Panel);
app.Slider2Label.HorizontalAlignment = 'right';
app.Slider2Label.Position = [104 156 42 22];
app.Slider2Label.Text = 'Slider2';
% Create Slider2
app.Slider2 = uislider(app.Panel);
app.Slider2.Position = [167 165 180 7];
% Create xLabel
app.xLabel = uilabel(app.Panel);
app.xLabel.Position = [41 215 52 33];
app.xLabel.Text = 'x';
% Create yLabel
app.yLabel = uilabel(app.Panel);
app.yLabel.Position = [41 148 44 39];
app.yLabel.Text = 'y';
% Create DropDownLabel
app.DropDownLabel = uilabel(app.Panel);
app.DropDownLabel.HorizontalAlignment = 'right';
app.DropDownLabel.Position = [41 281 65 22];
app.DropDownLabel.Text = 'Drop Down';
% Create DropDown
app.DropDown = uidropdown(app.Panel);
app.DropDown.Items = {'Sine signal', ''};
app.DropDown.ItemsData = {'y=sin(x)'};
app.DropDown.DropDownOpeningFcn = createCallbackFcn(app, @DropDownOpening, true);
app.DropDown.ValueChangedFcn = createCallbackFcn(app, @DropDownValueChanged, true);
app.DropDown.Position = [121 269 237 46];
app.DropDown.Value = 'y=sin(x)';

Answers (1)

Srijith Kasaragod
Srijith Kasaragod on 20 Sep 2021
Edited: Srijith Kasaragod on 20 Sep 2021
Assuming the x and y sliders adjust x-axis limit and y-axis limit of the plot respectively, following lines of code shows one of the possible solution to the problem.
The app contains dropdown, x-slider, y-slider, axes and a button. Dropdown enables selection of signals, i.e, either sin or cos. The x-slider and y-slider adjusts the x-axis limit and y-axis limit of the axes. Clicking on the push-button generates signal plot with axes limits as specified.
App variables required to hold user input are:
properties (Access = private)
func; % variable to hold the signal selected from dropdown
xslider; % variable to hold upper limit of xlim
yslider; % variable to hold upper limit of ylim
end
Startup function callback saves default values of dropdown and sliders into app variables.
function startupFcn(app)
app.func= str2func(app.SignalDropDown.Value); %str2func creates function handle from string
app.xslider= app.XSlider.Value;
app.yslider= app.YSlider.Value;
end
Signal dropdown callback function saves the user selected signal into app variable 'func'.
function SignalDropDownValueChanged(app, event)
value = app.SignalDropDown.Value;
app.func= str2func(value);
end
Pushbutton callback function holds the x, y values, generates the plot and sets xlim and ylim as per user input.
function EnterButtonPushed(app, event)
x=0:0.01:(360/180)*pi;
y= app.func(x);
plot(app.UIAxes,x,y);
xlim(app.UIAxes,[0 app.xslider]);
ylim(app.UIAxes,[-2 app.yslider]);
end
Slider callback functions saves user input into app variables 'xslider' and 'yslider'.
function XSliderValueChanged(app, event)
app.xslider = app.XSlider.Value;
end
function YSliderValueChanged(app, event)
app.yslider = app.YSlider.Value;
end
All components can be placed in the app as per user requirement. Refer this link to read more about 'str2func' function.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!