Processing the Data

The next callback we will work with is the start_pushbutton_Callback. This is probably the most complicated callback of this program because of all the subfunctions within it. Sub functions can be placed within the GUI m-file itself, or they can be placed in their own separate m-file as long as the m-files containing the sub functions are in the same directory as the GUI m-files. Once you’ve added the files that you want to process, we want to process the data. First, the callback will have to parse the input data files, and then plot the results onto the axes. How is this accomplished?

First, let’s add the code for the start_pushbutton_Callback:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
%get the list of input file names from the listbox
inputFileNames = get(handles.inputFiles_listbox,'String');
 
%checks to see if the user selected any input files
%if not, nothing happens
if isempty(inputFileNames)
    return
end
 
%disables the button while data is processing
disableButtons(handles);
%refresh the figure to reflect changes
refresh(data_processing_tool); 
 
%initialize the cell arrays
%if you don't know what cell arrays are, it might be a good idea to
%review this by using the Matlab Help Files
handles.data = {};
handles.legendData = {};
 
for x = 1 : length(inputFileNames)
    %gets the filename without the extension
    [ignore,fileName,ext,ignore]=fileparts(inputFileNames{x});
    %store filenames so that it will display on the legend
    handles.legendData(x) = {fileName};
    %stores the numerical data using the custom function 
    handles.data{x} = importMyData(inputFileNames{x});
end
 
handles.legendObject = plotData(handles.data,handles.legendData,handles.axes1,get(handles.plot_popupmenu,'Value'));
 
%the data must be done processing before other Callbacks will be 
%able to function properly. this variable will be used as a "check"
%to see whether the data has been processed or not
handles.processDataCompleted = 1;
 
%data is done processing, so re-enable the buttons
enableButtons(handles);
guidata(hObject, handles);
  1. The first thing you might notice is the disableButtons and enableButtons functions. Basically, these functions are included so that while Matlab is busy processing the data, the user cannot click on any of the other buttons. To learn more about this feature, click here to visit the post that covers this topic. The code for the two functions are:

    function disableButtons(handles)
    set(handles.figure1,'Pointer','watch');
    set(handles.start_pushbutton,'Enable','off');
    set(handles.reset_pushbutton,'Enable','off');
    set(handles.addFiles_pushbutton,'Enable','off');
    set(handles.savePlot_pushbutton,'Enable','off');
    set(handles.deleteFiles_pushbutton,'Enable','off');
    set(handles.inputFiles_listbox,'Enable','off');
    set(handles.plot_popupmenu,'Enable','off');
    set(handles.export_pushbutton,'Enable','off');
    function enableButtons(handles)
    set(handles.figure1,'Pointer','arrow');
    set(handles.start_pushbutton,'Enable','on');
    set(handles.reset_pushbutton,'Enable','on');
    set(handles.addFiles_pushbutton,'Enable','on');
    set(handles.savePlot_pushbutton,'Enable','on');
    set(handles.deleteFiles_pushbutton,'Enable','on');
    set(handles.inputFiles_listbox,'Enable','on');
    set(handles.plot_popupmenu,'Enable','on');
    set(handles.export_pushbutton,'Enable','on');
  2. The next function you might have questions about is the importMyData function. This function is a custom function that parses the input data. For more information on parsing data files, click here to visit the data parsing post. In this tutorial, the sample data files being parsed have three columns of data: frequency, magnitude, and phase. The parsing function takes an input file and outputs the frequency, magnitude, and phase data into a matrix. The following is the code for the parsing function.

    function [parsedData]= importMyData(name)
    %this function parses data files that have some header content
    %and some ending content
     
    %the input to this function is the file name of the data file.  
    %If the data file is not in the current Matlab directory
    %you must include the entire directory path.  
    %opens the file    
    fid = fopen(name);
     
    %reads line one by one into a cell
    a= 1;
    line = 0;
    while line ~= -1
        line = fgetl(fid);
        data{a} = line;
        a = a +1;
    end
    data(end) = []; %get rid of the last line
     
    fclose(fid);
     
    %this for loop determines where the numerical data starts
    for p=1:length(data)
        if (~isempty(str2num(data{p}))) break; end
    end
     
    %this loop saves the numerical data into parsedData
    %until the numerical data stops
    for x=p:length(data)
        if(isempty(str2num(data{x}))) break; end
        temp = str2num(data{x});
        parsedData(x-p+1,:) = temp; 
    end

    Note that if you plan to parse your own data which uses a different data file format, you will probably have to play around with the parsing function to get it to parse your data correctly. Also, notice how the data returned from importMyData is stored into a cell, and not a matrix. You can tell that it is a cell by the curly brackets used, {}. Cell arrays can be very versatile, so if you don’t know how to use them, you should consult the Matlab help!!

  3. Finally, the last custom function within this callback is the plotData function. Writing a separate custom function for plotting the data can be extremely useful because you can call it later on in any of the callbacks if you need to. This function returns the legend object of the plot so that it can be used later in other parts of this GUI. The function takes in 4 arguments. The data that will be plotted, the legend data, the axes that the data will be plotted on (this argument is helpful if you have more than 1 axes on your GUI), and the type of plot.

    function [legendObject]=plotData(data,legendData,axesName,option)
    cla(axesName); %clear the axes
    axes(axesName); %set the axes to plot
    hold on
    grid on
     
    %plot the magnitude plot
    if (option==1)
        for x=1:(length(data))
            plot(data{x}(:,1),data{x}(:,2));
     
        end
            %add a legend to the plot
            legendObject = legend(legendData,'Location','Best');
            title('Insert Title Here')
            xlabel('Frequency (GHz)')
            ylabel('Magnitude (dB)');
     
    %else plot the phase plot
    else
        for x=1:(length(data))
            plot(data{x}(:,1),data{x}(:,3));
        end
            %add a legend to the plot
            legendObject= legend(legendData,'Location','Best');
            title('Insert Title Here')
            xlabel('Frequency (GHz)')
            ylabel('Phase (Degrees)');
    end
    hold off
     
    %allow legend titles to be displayed properly
    set(legendObject,'Interpreter','none');

    Once again, this plot function is catered to the sample data files that I created. You will probably have to make some minor mods to it if you plan to use it to process other types of data files.

After adding in all of this code, it would be a good idea to test out the GUI. Run the GUI, add the input files, and process the data. You should get something that looks like this (click on figure to enlarge):

Data Processing GUI

Pages: 1 2 3 4 5 6 7