MATLAB Answers

Sahjtli
0

App Designer ListBox.Value working strangely?

Asked by Sahjtli
on 4 Oct 2018
Latest activity Edited by Cris LaPierre on 13 Nov 2018
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

Sign in to comment.

Products


Release

R2018a

1 Answer

Answer by Cris LaPierre on 11 Nov 2018
 Accepted Answer

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

Because I'm bored, I implemented your code. Same object names, etc. It works for me if I add curly braces to the assignment in the last line of your code
app.ySelectedListBox.Items(lastelement+1) = {app.yListListBox.Value}
One comment - I would make the if-else-end statement an if-elseif-end. That, or remove the logical test statement.
if app.TabGroup.SelectedTab == app.xTab
...
else
...
end
or
if app.TabGroup.SelectedTab == app.xTab
...
elseif app.TabGroup.SelectedTab == app.yTab
...
end
Thank you very much for your answer!
The problem was that one listbox allowed multiselect and the other didn't, and I wasn't aware that this setting can return different datatypes. (Somehow for me the multiselect only allows to select one entry only, so I couldn't notice the difference).
As for the if cycles, thank you for the feedback I changed it to if/ifelse combination, but I'm sure than many more mistakes like this are in the code, as I'm a beginner in Matlab and not even an expert programmer in other languages. :D
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.

Sign in to comment.