How can I use selections from multiple uidropdowns as function inputs?
이전 댓글 표시
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
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
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
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!