get string from listbox and set it as variable

2 ビュー (過去 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 Center および File ExchangeJust for fun についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by