MATLAB: How to populate a list box with random files/folders in My Computer Directory through GUIDE createfunction OR how to add a file in the listbox through call back function

adding/displaying files in listboxlist box popup

I have made a gui that can select a file(in my case .csv file) in a folder and I want to add/display this file in my listbox so that when I navigate through different folders then I can pick a file and add in the list box in my gui. Please tell me the stntax by which I can add/load/display my file in my list box.
Regards,

Best Answer

Suppose you have a button on your GUI (as developed in GUIDE) named (via its tag property) addFileButton, and suppose you have a list box named (via its tag property) fileListbox. Then in the button callback you can have the following code which, when called, will allow the user to select a file from his/her workstation and add that file to the listbox
% --- Executes on button press in addFileButton.
function addFileButton_Callback(hObject, eventdata, handles)
% hObject handle to addFileButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,path]=uigetfile('*.csv');
% use ischar to ensure that a file has been selected
% (so user hasn't pressed cancel)
if ischar(filename)
% concatenate the path with the filename
fullPathToFile = fullfile(path,filename);
% get the current set of files in the listbox
fileList = get(handles.fileListbox,'String');
% if first time, initialize the list as a cell array
if isempty(fileList)
fileList = {};
end
% add the file to the list
fileList = [fileList ; fullPathToFile];
% set the file list back in the listbox
set(handles.fileListbox,'String',fileList);
end
And that is it. Try the above and see what happens!