Add Files and Delete Files Callback Functions

The first two callbacks that we are going to program are addFiles_pushbutton_Callback and deleteFiles_pushbutton_Callback.

  1. Add the following code under the addFiles_pushbutton_Callback:

    %gets input file(s) from user. the sample data files have extension .s2p
    [input_file,pathname] = uigetfile( ...
           {'*.s2p', 'Data Files (*.s2p)'; ...
            '*.*', '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 iscell(input_file) == 0
     
        %add the most recent data file selected to the cell containing
        %all the data file names
        inputFileNames{length(inputFileNames)+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)
            inputFileNames{length(inputFileNames)+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);
  2. But what happens if you accidentally added too many files? This is what the delete button is for! Add the following code underneath the deleteFiles_pushbutton_Callback:

    %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 )
        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);

    To allow multiple files to be selected within the listbox, you must add the following code to the opening function. Alternatively, you can change these properties through the Property Inspector using GUIDE.

    set(handles.inputFiles_listbox,'Max',2);
    set(handles.inputFiles_listbox,'Min',0);
  3. After you have added in the code, you should test the GUI to make sure that it works. There are some sample test files included in the zip file that you just downloaded. You can try adding/removing these files to the GUI. The menu below pops up when you click on the “Add Files” button.

    Data Processing GUI - Adding Files

Pages: 1 2 3 4 5 6 7