How to call a Variable from a List Dialog Box

조회 수: 11 (최근 30일)
Robert Jack
Robert Jack 2020년 3월 5일
편집: Robert Jack 2020년 3월 5일
Hey everyone,
Im trying to get the values out of a variable which I have defined already but I don't call them unless I select it from a list.
Var1 = [10, 5, -1, 2, 3]
Var2 = [5, 6, 7, 8]
... etc
List = {'Var1','Var2'};
[indx] = listdlg('ListString',List,'SelectionMode','Single')
% Selected the variable from list
VarChosen = List{indx}
%VarChosen = 'Var1'
So here when I select it i get a Char of 'Var1'
What I need is the values of Var 1 so that
Var1_A = Var1(1)
Var1_B = Var1(2)
Var_1C = Var1(3)... etc
I think i'm just missing a way for the Character Array to be seen as the variable for it to call.. I don't know.
Any help would be appreciated.

채택된 답변

Robert Jack
Robert Jack 2020년 3월 5일
편집: Robert Jack 2020년 3월 5일
30 minutes later and I have found out how to solve it
Basically I had to make my Variables into a Structure instead of trying to dynamically calling them
S.Var1 = [10, 5, -1, 2, 3]
S.Var2 = [5, 6, 7, 8]
List = fieldnames(S)
[indx] = listdlg('ListString',List,'SelectionMode','Single')
Var_Chosen = S.(List{indx})
Var1_A = Var_Chosen(1)
This gave me the correct value from the Variable which I was looking for
Var1_A = 10

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by