Automatically upload a folder of files into a listBox
2 views (last 30 days)
Show older comments
Emanuele Gandola
on 6 Oct 2015
Commented: Emanuele Gandola
on 6 Oct 2015
Hallo! I'd like to allow the GUI user to automatically select all .jpg file from a folder. I have been implemented a checkbox called check_Folder, if it is checked I'd like to upload into the listbox1 all the jpeg files that are into the folder in which is the one selected.
in the else case it works perfectlly
I've been implemented this if routine
-
[filenamesCell, pathname] = uigetfile({'*.jpg'},'Select file(s)','MultiSelect');
if get(handles.check_Folder , 'Value') == true
lista = dir ('pathname*.jpg');
for i = 1: length(lista)
oldlist = cellstr( get(handles.listbox1, 'String') );
filenamesCell = lista(i,1).name
set(handles.listbox1, 'String', [oldlist; fullfile(pathname, filenamesCell)]);
end
else
if ~isnumeric(filenamesCell)
oldlist = cellstr( get(handles.listbox1, 'String') );
set(handles.listbox1, 'String', [oldlist; fullfile(pathname, filenamesCell)]);
end
end
I've check into the command window that
lista = dir ('pathname*.jpg');
and
filenamesCell = lista(i,1).name;
works, but into the GUI nothing happends. And at the end I'd like to give in input (Through an execute Push_button) the list generated into the ListBox to a main fuction to elaborate the images all toghether.
Thanks 2000 times for your help
0 Comments
Accepted Answer
Image Analyst
on 6 Oct 2015
In the click callback event for your listbox, have this:
% Get a list of indexes in the listbox that the user has selected.
Selected = get(handles.listbox1, 'value');
% If more than one is selected, bail out. OPTIONAL. DELETE IF YOU WANT TO ALLOW MULTIPLE SELECTIONS.
if length(Selected) > 1
return;
end
% If you get to here, exactly 1 is selected.
% Get a list of all the items in the listbox.
ListOfImageNames = get(handles.listbox1, 'string');
% Get the name(s) of the particular file(s) that has (have) been selected.
imageName = strcat(cell2mat(ListOfImageNames(Selected)));
1 Comment
Image Analyst
on 6 Oct 2015
Actually you can call that code anywhere, like in the callback of a "Go!" button that will batch process the images.
More Answers (1)
Adam
on 6 Oct 2015
Edited: Adam
on 6 Oct 2015
get(handles.check_Folder , 'Value') == true
will not work as you want. A checkbox value is a double so you would need to do:
logical( get(handles.check_Folder , 'Value') ) == true
to get into your if statement with the correct condition.
if get(handles.check_Folder , 'Value')
...
end
should work too based on implicit conversion of the double to a logical.
This is an annoying effect of all ui components being the same uicontrol class rather than specific ones, so you end up with text controls having a SliderStep too!
3 Comments
Adam
on 6 Oct 2015
Edited: Adam
on 6 Oct 2015
Ah yes, that is true actually for 0 and 1. I just happened to test with '3' as my double instead of '1' and unlike in usual logical conditions where 3 is interpreted as 'true'
3 == true
does not return true. Since a checkbox should always be 1 or 0 though the logic does work I guess.
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!