MATLAB Answers

Sahjtli
0

App Designer ListBox.Value working strangely?

Asked by Sahjtli
on 4 Oct 2018
Latest activity Commented on by Cris LaPierre on 11 Nov 2018 at 4:26
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 at 3:47

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.

  1 Comment

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

Sign in to comment.