get string from listbox and set it as variable
3 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Walter Roberson
on 20 Sep 2015
POP=[1 2 3 4];
ROCK=[7 8 9 10];
C = {POP, ROCK};
M = get(handles.listbox2,'Value');
plot(C{M},Y);
0 Comments
Jan
on 20 Sep 2015
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 Comments
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!