How can I use selections from multiple uidropdowns as function inputs?

10 views (last 30 days)
I'm using two (2) uidropdowns with a valuechangedfcn to live update the x-y axes for a plot based on the selected variable. I can't figure out how to simultaneously pass the information from both dropdowns to my plotting function. For example, say I have data for three variables: time, position, and velocity, and I have two drop downs each containing the same three variables for the x and y axes. I'm unable to retain a previous variable selection for the x-axis once I select a new variable for the y-axis, so it just reverts to the initialized x-variable. Is there a way to store the selection from each dropdown simultaneously

Answers (2)

Walter Roberson
Walter Roberson on 8 Jul 2024
Inside the plotting function, access the uidropdown value property directly for the two controls.
xval = app.XDropDown.Value;
yval = app.YDropDown.Value;

Voss
Voss on 8 Jul 2024
Here's an example of how it could work:
function uidropdown_demo()
T = array2table([(1:25).' [10 100].*rand(25,2)], ...
'VariableNames',{'time','position','velocity'});
f = uifigure();
ax = uiaxes(f, ...
'Position',[20 20 400 300]);
xdd = uidropdown(f, ...
'Items',T.Properties.VariableNames, ...
'Position',[20 340 100 26], ...
'ValueChangedFcn',@vcf);
ydd = uidropdown(f, ...
'Items',T.Properties.VariableNames, ...
'Position',[220 340 100 26], ...
'ValueChangedFcn',@vcf);
vcf() % initialize the plot
function vcf(~,~)
plotFunction(xdd.Value,ydd.Value)
end
function plotFunction(x,y)
cla(ax)
plot(ax,T.(x),T.(y));
xlabel(ax,x);
ylabel(ax,y);
end
end

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!