get string from listbox and set it as variable

2 visualizzazioni (ultimi 30 giorni)
Anisa Corina
Anisa Corina il 20 Set 2015
Risposto: Jan il 20 Set 2015
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?

Risposte (2)

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

Jan
Jan il 20 Set 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.

Categorie

Scopri di più su Just for fun in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by