App Designer ListBox.Value working strangely?
6 views (last 30 days)
Show older comments
Hi all!
I encountered the following problem: I tried to import file names from 2 different folders to 2 listboxes. Until this the code works. But then I tried to add the selected items from the respective lists to another 2 lists. So until this the code should be totally the same.
But in one version the ListBox.Value is a string and in the other is a cell array, and I haven't been able to find the cause.
Does anybody have an explanation for this phenomenon or it is just a bug?
The code:
if true
% code
end
searchpath = (['\\xxx\yyy\zzz\' app.TabSelect])
listmeasdata = dir(searchpath)
dirFlags = [listmeasdata.isdir]
files = {listmeasdata(~dirFlags).name}
if app.TabGroup.SelectedTab == app.xTab
app.xListListBox.Items = files
else app.TabGroup.SelectedTab == app.yTab
app.yListListBox.Items = files
end
function TabGroupSelectionChanged(app, event)
if app.TabGroup.SelectedTab == app.xTab
app.TabSelect = 'x'
end
if app.TabGroup.SelectedTab == app.yTab
app.TabSelect = 'y'
% Button pushed function: xAddtoselectionButton
function xAddtoselectionButtonPushed(app, event)
lastelement = numel(app.xSelectedListBox.Items)
app.xSelectedListBox.Items(lastelement+1) = {app.xListListBox.Value}
% Button pushed function: yAddtoselectionButton
function yAddtoselectionButtonPushed(app, event)
lastelement = numel(app.ySelectedListBox.Items)
app.ySelectedListBox.Items(lastelement+1) = app.yListListBox.Value
You can see my problem in the first and fifth row from the bottom. Neither
- app.xSelectedListBox.Items(lastelement+1) = app.xListListBox.Valueor
- app.ySelectedListBox.Items(lastelement+1) = {app.yListListBox.Value}works
0 Comments
Accepted Answer
Cris LaPierre
on 11 Nov 2018
The second case should work.
app.ySelectedListBox.Items(lastelement+1) = {app.yListListBox.Value}
To test, I made a simple app with 4 listboxes and 2 buttons. Button2 selects the files and adds them to all listboxes. Button3 adds the selected items from box 1 and 2 and adds them to the bottoms of boxes 3 and 4. It worked.
% Button pushed function: Button2
function Button2Pushed(app, event)
files = uigetfile('MultiSelect','on');
app.ListBox.Items = files;
app.ListBox2.Items = files;
app.ListBox3.Items = files;
app.ListBox4.Items = files;
end
% Button pushed function: Button3
function Button3Pushed(app, event)
l = numel(app.ListBox3.Items)
app.ListBox3.Items(l+1) = {app.ListBox.Value};
app.ListBox4.Items(l+1) = {app.ListBox2.Value};
end
One thing to be aware of in this simplified example. You must select at least 2 files since uigetfile returns a char array if one file is selected and a cell array if more than one are selected.
3 Comments
Cris LaPierre
on 13 Nov 2018
Edited: Cris LaPierre
on 13 Nov 2018
Keep playing around! If you need any help learning MATLAB, consider going through MATLAB Onramp. It is designed to help brand new users get familiar with MATLAB syntax.
For multiselect, the user would ctrl+click on items in the list to select multiple items. The assignment would have to be made differently, however (you'd have to know how many items are being added, and adjust the assignment indexing accordingly). I would consider it simpler and more robust to do the following (using your nomenclature):
% Button pushed function: xAddtoselectionButton
function xAddtoselectionButtonPushed(app, event)
app.xSelectedListBox.Items = [app.xSelectedListBox.Items app.xListListBox.Value];
end
This replaces all the items in the listbox each time, but works if a single item is selected or multiple items. So the same code would work whether multiselection is allowed or not.
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks 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!