How to populate listbox with all files in a folder - GUI

2 visualizaciones (últimos 30 días)
Ellis Berry
Ellis Berry el 2 de Mzo. de 2016
Comentada: david martinez el 1 de Jul. de 2017
Hi everyone, I have a problem with trying to populate the listbox in a GUI. I want to click a pushbutton 'browse', select any folder on my computer and have the GUI load all the files in that folder to a listbox (listbox1) so the user can see all the files in that folder. I asked this in an earlier question but cannot comment on the answer so started a new question. So far, thanks to Orion, I have this code under the pushbutton callback function:
% get the folder
folder_name = uigetdir;
% get what is inside the folder
Infolder = dir(folder_name);
% Initialize the cell of string that will be update in the list box
MyListOfFiles = [];
% Loop on every element in the folder and update the list
for i = 1:length(Infolder)
if Infolder(i).isdir==0
MyListOfFiles{end+1,1} = Infolder(i).name;
end
end
% update the listbox with the result
set(handles.listbox1,'String',MyListOfFiles)
When I ran the GUI, I clicked browse, chose a folder and I received this error:
Struct contents reference from a non-struct array object.
Error in GUI_2>pushbutton1_Callback (line 120) set(handles.listbox1,'String',MyListOfFiles)
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in GUI_2 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI_2('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Any ideas? I feel like I should have some code under the listbox createfcn and listbox callback functions too?
Cheers, Ellis
  3 comentarios
Stephen23
Stephen23 el 2 de Mzo. de 2016
PS: If you had stuck with your original question you would have learned that you can replace most of your code (the loops) with these two lines:
S = dir(folder_name);
N = {S(~[S.isdir]).name}
This is what happens when people ask the same question in multiple locations: they lose track of what info that have been given.
SANKAR JYOTI NATH
SANKAR JYOTI NATH el 27 de Oct. de 2016
sir i have used your code and it works perfectly but as a result i found only the files name not the data which contain by the files....i want to open the files in a listbox in GUI...need your help

Iniciar sesión para comentar.

Respuestas (1)

Orion
Orion el 2 de Mzo. de 2016
Seems you have a problem with the structure handles
The code I showed earlier was made using a gui developped with GUIDE.
I attached the demo gui I made. You can use it to compare with your code and see what's wrong.
  1 comentario
david martinez
david martinez el 1 de Jul. de 2017
Working diligent
You can help me just to read the file name, not the address

Iniciar sesión para comentar.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by