Introduction

Matlab Logo In many applications, the user must specify the input. In this tutorial, you will learn how to use the listbox component and the uigetfile command to create an attractive input interface. Specifically, you will learn how to create a GUI that will allow the user to select multiple files and display all the file names inside the listbox. I’ve done a similar example before in the basic processing tool, but I would like to go over this portion in a little more detail. Here’s a picture of the GUI:

GUI

The Example Files and Code

  1. First, download the GUI skeleton here. Unzip the files and place them wherever you please. I also included 4 sample excel files that you can use to test the GUI.

  2. Now, type guide at the command prompt.

    Command prompt

  3. Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

    GUIDE Screen

  4. Here is what the GUI should look like when you open it:

    GUI Figure

  5. Click on the mfile icon icon on the GUI figure to bring up the accompanying .m file.

  6. Now, add the following code to addFile_pushbutton_Callback. As the name suggests, this is the callback that will add the files to the listbox. For this tutorial, the only file type that I allowed to be added were excel files (.xls).

     
    %gets input file(s) from user
    [input_file,pathname] = uigetfile( ...
           {'*.xls', 'Excel (*.xls)'; ...
            '*.*', 'All Files (*.*)'}, ...
            'Select files', ... 
            'MultiSelect', 'on');
     
    %if file selection is cancelled, pathname should be zero
    %and nothing should happen
    if pathname == 0
        return
    end
     
    %gets the current data file names inside the listbox
    inputFileNames = get(handles.inputFiles_listbox,'String');
     
    %if they only select one file, then the data will not be a cell
    %if more than one file selected at once,
    %then the data is stored inside a cell
    if iscell(input_file) == 0
     
        %add the most recent data file selected to the cell containing
        %all the data file names
        inputFileNames{end+1} = fullfile(pathname,input_file);
     
    %else, data will be in cell format
    else
        %stores full file path into inputFileNames
        for n = 1:length(input_file)
            %notice the use of {}, because we are dealing with a cell here!
            inputFileNames{end+1} = fullfile(pathname,input_file{n});
        end
    end
     
    %updates the gui to display all filenames in the listbox
    set(handles.inputFiles_listbox,'String',inputFileNames);
     
    %make sure first file is always selected so it doesn't go out of range
    %the GUI will break if this value is out of range
    set(handles.inputFiles_listbox,'Value',1);
     
    % Update handles structure
    guidata(hObject, handles);
  7. Next, we need to add the following code to the deleteFile_pushbutton_Callback. This code will allow you to delete any files you may have accidentally added into the listbox.

    %get the current list of file names from the listbox
    inputFileNames = get(handles.inputFiles_listbox,'String');
     
    %get the values for the selected file names
    option = get(handles.inputFiles_listbox,'Value');
     
    %is there is nothing to delete, nothing happens
    if (isempty(option) == 1 || option(1) == 0 || isempty(inputFileNames))
        return
    end
     
    %erases the contents of highlighted item in data array
    inputFileNames(option) = [];
     
    %updates the gui, erasing the selected item from the listbox
    set(handles.inputFiles_listbox,'String',inputFileNames);
     
    %moves the highlighted item to an appropiate value or else will get error
    if option(end) > length(inputFileNames)
        set(handles.inputFiles_listbox,'Value',length(inputFileNames));
    end
     
    % Update handles structure
    guidata(hObject, handles);
  8. Before we add any code to the final pushbutton, let’s make sure that what we have done so far works. Now, save your .m file and run the GUI. You should see the following GUI appear

    GUI

  9. Press the Add File(s) button. You should see the following dialog box appear. Select all of the files at once.

    Select Files

  10. Now you should see the following on your GUI. The selected files will appear in the listbox! Try using the delete pushbutton to delete some of the files you selected.

    Select Files

  11. Now that you can select the input files, it might be a good idea if we actually did something with them. So let’s add some code that will read the data! Go back to the m-file, and add the following code to the readData_pushbutton_Callback.

    %get the current list of file names from the listbox
    inputFileNames = get(handles.inputFiles_listbox,'String');
     
    for x=1:length(inputFileNames)
        %display the file name on the command prompt
        inputFileNames{x}
        %display the excel data on the display prompt
        xlsread(inputFileNames{x})
    end
  12. Now, run your GUI again, add the excel files, and press the read data button. You should see the output on the Matlab command prompt! I showed some of the output here in the following picture.

    Select Files

Download Source Files

Click here to download the source files.

This is the end of the tutorial.