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

5 次查看(过去 30 天)
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

回答(2 个)

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

Voss
Voss 2024-7-8
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

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by