index of the items in a listbox

28 ビュー (過去 30 日間)
acegi bdegi
acegi bdegi 2018 年 1 月 8 日
編集済み: acegi bdegi 2018 年 1 月 8 日
Using app designer, is it possible to get the index of the selected items from the listbox?
Example: I have 6 items in the listbox; data1, data2, vector1, vector2, vector3 and emc.
If data1, vector1, vector2 and emc are selected, I want to read the location of these items in the listbox, which is 1, 3, 4, 6.

採用された回答

Guillaume
Guillaume 2018 年 1 月 8 日
I'm not sure what you mean by order. The value property of the listbox tells you which items are selected, so in your example you'll get {'data1', 'vector1', 'vector2', 'emc'}. If you want the index of selected elements, you can either pass your selection to ismember and get its second output, or fill the ItemsData property with the indices:
  • using ismember:
listitems = {'data1', 'data2', 'vector1', 'vector2', 'vector3', 'emc'};
hfig = uifigure;
hbox = uilistbox(hfig, 'Items', listitems, 'Multiselect', 'on');
hbox.Value = {'data1', 'vector1', 'vector2', 'emc'}; %simulate user selection
%get index of selected values
[~, index] = ismember(hbox.Value, hbox.Items)
  • using ItemsData:
listitems = {'data1', 'data2', 'vector1', 'vector2', 'vector3', 'emc'};
hfig = uifigure;
hbox = uilistbox(hfig, 'Items', listitems, 'ItemsData', 1:numel(listitems), 'Multiselect', 'on');
hbox.Value = [1 3 4 6]; %simulate user selection
%get index of selected values
index = hbox.Value %returns corresponding ItemsData
  1 件のコメント
acegi bdegi
acegi bdegi 2018 年 1 月 8 日
編集済み: acegi bdegi 2018 年 1 月 8 日
I indeed meant the index. Edited.
It works, thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by