get string from listbox and set it as variable

조회 수: 3 (최근 30일)
Anisa Corina
Anisa Corina 2015년 9월 20일
답변: Jan 2015년 9월 20일
Hi, I have problem to get selection from listbox and set it as variable to read data I already set.The objective here is to plot my selection with my Y with a pushbutton. So, in my GUI: Under my listbox I set this:
set(handles.listbox1,'String',{'POP','ROCK'});
Under pushbutton I set this:
POP=[1 2 3 4];
ROCK=[7 8 9 10];
Y=[ 1 2 3 4];
C = get(handles.listbox2,'String');
M = get(handles.listbox2,'Value');
plot(C{M},Y);
In the end, matlab fails to recognize POP and ROCK that I already set. Is there any other option rather that I have to change the variable name POP into C{1} and etc?

답변 (2개)

Walter Roberson
Walter Roberson 2015년 9월 20일
POP=[1 2 3 4];
ROCK=[7 8 9 10];
C = {POP, ROCK};
M = get(handles.listbox2,'Value');
plot(C{M},Y);

Jan
Jan 2015년 9월 20일
plot(C{M},Y);
Now C{M} is the string 'ROCK' or 'POP', but not the corresponding variable. Using a struct might be useful:
Data.POP = [1 2 3 4];
Data.ROCK = [7 8 9 10];
Y = [1 2 3 4];
C = get(handles.listbox2,'String');
M = get(handles.listbox2,'Value');
plot(Data.(C{M}), Y);
See "dynamic fieldnames" in the docs or in this forum.

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by