best practice for multi-selection listbox

7 ビュー (過去 30 日間)
Josh
Josh 2015 年 1 月 8 日
コメント済み: Josh 2015 年 1 月 9 日
After the user selects some entries from a listbox, what is the "matlab" way of creating an array that will have all the selected items? There is, of course, the straightforward procedural approach of building it of the array that is returned, and there is a way of building a boolian array with "1" at the selected entries (by looping over the returned array) and than doing something like:
FileList=FileList(BoolArray>0)
which is working allright, but I have a strong feeling there is a better way of doing it with MATLAB, since that "looping over the returned array" seems to be crying to be changed.
BTW, in the above example trying to use the boolian array without the ">0" - i.e. FileList=FileList(BoolArray) - generates a syntax error, which is probably avoidable somehow...?
  2 件のコメント
Jan
Jan 2015 年 1 月 8 日
If you mention an error, please post the complete message.
Josh
Josh 2015 年 1 月 9 日
Right... FileList=FileList(BoolArray) gives:
"Subscript indices must either be real positive integers or logicals."

サインインしてコメントする。

採用された回答

Adam
Adam 2015 年 1 月 8 日
編集済み: Adam 2015 年 1 月 8 日
Just getting the string property from the list box and applying logical indexing based on the listbox's value seems a perfectly natural way of doing this.
e.g.
h = uicontrol( 'Style', 'listbox', 'max', 10, 'Units', 'normalized', 'Position', [0.1 0.1 0.8 0.8], 'String', { 'option1', 'option2', 'option3', 'option4' } )
h.String( h.Value )
ans =
'option2'
'option4'
after selecting 2 and 4.
It is true though that uicontrols are not especially friendly, with the same base for all different types of uicontrol with properties interpreted in different ways and some (e.g. SliderStep) just blatantly irrelevant in almost all cases!

その他の回答 (1 件)

Jan
Jan 2015 年 1 月 8 日
  1 件のコメント
Josh
Josh 2015 年 1 月 8 日
Good to know, thanks, I might use it. Adam's answer is exactly what I was looking for, though (one line of code, huh!).

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by