get string from listbox and set it as variable
조회 수: 3 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (2개)
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);
댓글 수: 0
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.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!