Multiple drop down menus plotted on same graph.

9 views (last 30 days)
steve geddes
steve geddes on 6 Jun 2021
Answered: Aashray on 25 Aug 2025 at 6:56
Is there a way to plot two sets of data on the same plot so they update based on the drop down menus?
So instead of colour for the one drop down menu you could select a second function.
Meaning you could select cos and sin to display at the same time. Then you could change one to tan or whatever else?
function plotOptions
fig = uifigure;
fig.Position(3:4) = [440 320];
ax = uiaxes('Parent',fig,...
'Position',[10 10 300 300]);
x = linspace(-2*pi,2*pi);
y.A = sin(x);
y.B = cos(x);
y.C = sinc(x);
y.D = tan(x);
y.E = exp(x);
p = plot(ax,x,y.A);
p.Color = 'Blue';
dd = uidropdown(fig,...
'Position',[320 160 100 22],...
'Items',{'Red','Yellow','Blue','Green'},'Value','Blue',...
'ValueChangedFcn',@(dd,event) selection(dd,p));
N = uidropdown(fig, 'Position',[320 100 100 22],...
'Items',{'sin()','cos()','sinc()','tan()', 'exp()'},'Value','sin()', ...
'ValueChangedFcn', @(N, event) YD(N, p, y));
end
% Create ValueChangedFcn callback:
function selection(dd,p)
val = dd.Value; p.Color = val;
end
function YD(N, p, y)
switch N.Value
case 'sin()'
val = y.A;
case 'cos()'
val = y.B;
case 'sinc()'
val = y.C;
case 'tan()'
val = y.D;
case 'exp()'
val = y.E;
otherwise
error('WHAT?!')
end
p.YData = val;
end

Answers (1)

Aashray
Aashray on 25 Aug 2025 at 6:56
If you want to display two functions on the same axes and let each function be controlled by its own dropdown menu (for both function choice and colour), you can do this by creating two line objects and wiring each to a pair of dropdowns. The callbacks then just update the “YData” (for the function) or “Colour” (for the style) of the corresponding line.
You can refer to the following script for better understanding:
function plotOptions
fig = uifigure;
fig.Position(3:4) = [520 360];
ax = uiaxes('Parent',fig,'Position',[10 10 360 340]); grid(ax,'on'); hold(ax,'on')
x = linspace(-2*pi,2*pi);
y.A = sin(x);
y.B = cos(x);
y.C = sinc(x);
y.D = tan(x);
y.E = exp(x);
% Two plotted series
p1 = plot(ax,x,y.A,'DisplayName','sin()');
p1.Color = 'Blue';
p2 = plot(ax,x,y.B,'DisplayName','cos()');
p2.Color = 'Red';
hold(ax,'off')
legend(ax,'show');
% Series 1 controls
uidropdown(fig,'Position',[380 260 120 22],...
'Items',{'Red','Yellow','Blue','Green','Magenta','Cyan','Black'},...
'Value','Blue',...
'ValueChangedFcn',@(dd,~) selection(dd,p1));
uidropdown(fig,'Position',[380 290 120 22],...
'Items',{'sin()','cos()','sinc()','tan()','exp()'},...
'Value','sin()', ...
'ValueChangedFcn', @(N,~) YD(N,p1,y,ax)); % pass ax
% Series 2 controls
uidropdown(fig,'Position',[380 170 120 22],...
'Items',{'Red','Yellow','Blue','Green','Magenta','Cyan','Black'},...
'Value','Red',...
'ValueChangedFcn',@(dd,~) selection(dd,p2));
uidropdown(fig,'Position',[380 200 120 22],...
'Items',{'sin()','cos()','sinc()','tan()','exp()'},...
'Value','cos()', ...
'ValueChangedFcn', @(N,~) YD(N,p2,y,ax)); % pass ax
end
function selection(dd,p)
p.Color = dd.Value;
end
function YD(N, p, y, ax)
switch N.Value
case 'sin()', val = y.A;
case 'cos()', val = y.B;
case 'sinc()', val = y.C;
case 'tan()', val = y.D;
case 'exp()', val = y.E;
otherwise, error('Unknown selection');
end
p.YData = val;
p.DisplayName = N.Value;
legend(ax,'show');
end
When you run this function, you’ll see both curves plotted initially ("sin" in blue and "cos" in red). Each series has its own two dropdown menus, one for function selection and one for colour selection. Changing any dropdown updates only that line without affecting the other.
You can refer to the documentation for these UI components here:

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!