Is there a way to select a variable from a drown down menu rather than a string?

조회 수: 1 (최근 30일)
I want to have something like this:
x = 1;
y = 2;
z = 3;
% drop down menu options are x, y, or z.
dropdownValue = x;
disp(dropdownValue);
dropdownValue
= 1
what I actually get is:
x = 1;
y = 2;
z = 3;
% drop down menu options are x, y, or z.
dropdownValue = 'x';
disp(dropdownValue);
dropdownValue
= x
I'm struggling to describe it very well but hope this kind of sudo-code helps!

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 5일
var_values = [x; y; z];
dropdownIDX = handles.AppropriateHandleName.Value;
dropdownValue = var_values(dropdownIDX);
That is, the Value property of a uicontrol 'style', 'drop' is the index of the selected entry. You would use that index to index into the list of values to get the selected value.
You would not use dynamic variable names.
However, you could also use
var_values.x = x; var_values.y = y; var_values.z = z;
dropdownEntries = handles.AppropriateHandleName.String;
dropdownIDX = handles.AppropriateHandleName.Value;
dropDownString = dropdownEntries{dropdownIDX}; %this is the part that would get you 'x'
dropdownValue = var_values.(dropDownString);
  댓글 수: 1
Eddie Martin
Eddie Martin 2020년 8월 5일
편집: Eddie Martin 2020년 8월 5일
Thanks you for your help!
var_values = [x; y; z];
dropdownIDX = handles.AppropriateHandleName.Value;
dropdownValue = var_values(dropdownIDX);
So when using this, what value does dropdownIDX take? In other words what should be the options in the drop down?
Edit: The second method I understand a little better and have edited it slightly to work in my real code. There are three drop downs. In each, the user selects what axis they would like to change the original axis to. So app.xChangetoDropDown.Value should = 'y' for ease of the user, but in the code it will be refered to as 2. I dont know how robust this is but it seems to work as intended with minimal testing so far.
x = 1;
y = 2;
z = 3;
var_values.x = x; var_values.y = y; var_values.z = z;
%x
xdropDownString = app.ChangextoDropDown.Value;
xChangeto = var_values.(xdropDownString);
%y
ydropDownString = app.ChangeytoDropDown.Value;
yChangeto = var_values.(ydropDownString);
%z
zdropDownString = app.ChangeztoDropDown.Value;
zChangeto = var_values.(zdropDownString);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by