Matlab App Designer ListBox.Value changes when ItemsData is an array
47 views (last 30 days)
Show older comments
Hi All,
I am having a problem with app designer LisxtBox. I have a ListBox and I define the names of my items in a cell array (for example; ch1, ch2, ch3, ch4). When I use the following commands, I can retrieve the selected item or its index
app.ListBox.Value
[~,index] = ismember(app.ListBox.Value,app.ListBox.Items)
However, when I attribute (match) the items with an array (for example; ch1 becomes Nx1, N being the number of data points. I use app.ListBox.ItemsData for defining each items data array), automatically 'app.ListBox.Value' becomes 'Nx1' cell array.
My intention is only be able to retrive the name of the item I selected (when I click on listbox item, I only want to see ch1, ch2 etc., not Nx1 array). Should I use something else rather than 'ListBox.Value'? Can you please help me with that?
Thank you.
0 Comments
Accepted Answer
Adam Danz
on 15 May 2020
Edited: Adam Danz
on 15 May 2020
I think what you're looking for is,
[~,index] = ismember(app.ListBox.Value, app.ListBox.ItemsData); % Note: ItemsData
% This returns a character vector; it won't work with >1 selection
value = app.ListBox.Items{index};
% This returns a cell containing char-vectors
value = app.ListBox.Items(index);
7 Comments
Adam Danz
on 15 May 2020
"Function you provided gives 1x4 array with zeros inside."
The function returns a logical vector. If the vector is all 0s (all False), that means there are no matches. That could occur for one of three reasons.
- No items are selected within the listbox
- There's items in ItemData aren't merely Nx1 arrays as described
- There are rounding issues with floating point precision.
Here's a demo that my solution should work.
fig = uifigure();
lbx = uilistbox(fig,'Items',{'Apples','Pears','Bananas'}, ...
'ItemsData',{rand(1,4095)',rand(1,4095)',rand(1,4095)'})
index = cellfun(@(c)isequal(lbx.Value,c), lbx.ItemsData)
% RESULT:
% index =
% 1×3 logical array
% 1 0 0
find(index) % = 1 (1st row of list box)
If this approach doesn't work, I can't trouble shoot it without a minimal working example (ie, you can share a simple listbox that reproduces the problem).
But first, let's take a step back. It would be nice if listbox returned an index number of the user's selection. Matlab should implement this!. But in the mean time, you can reorganize and simplify your workflow so that you can use the listbox selection to get the values in ItemsData after the selection is made.
Here's an example adapted from my example above.
% Create a cell array of ListBox options and your data.
% This could also be organized within a table if your arrays are
% all the same lenght.
data = {
'Ch1', rand(1,4095)';
'Ch2', rand(1,4095)';
'Ch3', rand(1,4095)'};
fig = uifigure();
lbx = uilistbox(fig,'Items', data(:,1));
% Get user's selection
[~,index] = ismember(lbx.Value, lbx.Items);
% Get associated data
chosenData = data{index,2};
More Answers (0)
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!