Pop-up Menu and Save Plot Callbacks

  1. The pop-up menu is relatively easy to use and allows the user to select between displaying the magnitude plot or the phase plot on the axes. Add the following code to plot_popupmenu_Callback

    %if the data hasn't been processed yet, 
    %nothing happens when this button is pressed
    if (handles.processDataCompleted == 0)
        return
    end
     
    %get the value of the current Pop-up menu selection
    %plotType = 1, if magnitude option is chosen
    %plotType = 2, if phase option is chosen
    plotType = get(hObject,'Value');
     
    %plots the data
    handles.legendObject = plotData(handles.data,handles.legendData,handles.axes1,plotType);
    guidata(hObject, handles);

    Click here if you need a refresher on how Pop-up Menus work.

  2. The next callback is the savePlot_pushbutton_Callback. For more information on this, you can read this post here. In this callback, we put the handles.legendObject to use. The handles.legendObject, if you remember, is an output from the plotData function.

    %if the data hasn't been processed yet, 
    %nothing happens when this button is pressed
    if (handles.processDataCompleted == 0)
        return
    end
     
    disableButtons(handles);
    refresh(data_processing_tool);
    savePlotWithinGUI(handles.axes1,handles.legendObject);
    enableButtons(handles);
    guidata(hObject, handles);
    function savePlotWithinGUI(axesObject, legendObject)
    %this function takes in two arguments
    %axesObject is the axes object that will be saved (required input)
    %legendObject is the legend object that will be saved (optional input)
     
    %stores savepath for the phase plot
    [filename, pathname] = uiputfile({ '*.emf','Enhanced Meta File (*.emf)';...
            '*.bmp','Bitmap (*.bmp)'; '*.fig','Figure (*.fig)'}, ... 
            'Save picture as','default');
     
    %if user cancels save command, nothing happens
    if isequal(filename,0) || isequal(pathname,0)
        return
    end
    %create a new figure
    newFig = figure;
     
    %get the units and position of the axes object
    axes_units = get(axesObject,'Units');
    axes_pos = get(axesObject,'Position');
     
    %copies axesObject onto new figure
    axesObject2 = copyobj(axesObject,newFig);
     
    %realign the axes object on the new figure
    set(axesObject2,'Units',axes_units);
    set(axesObject2,'Position',[15 5 axes_pos(3) axes_pos(4)]);
     
    %if a legendObject was passed to this function . . .
    if (exist('legendObject'))
     
        %get the units and position of the legend object
        legend_units = get(legendObject,'Units');
        legend_pos = get(legendObject,'Position');
     
        %copies the legend onto the the new figure
        legendObject2 = copyobj(legendObject,newFig);
     
        %realign the legend object on the new figure
        set(legendObject2,'Units',legend_units);
        set(legendObject2,'Position',[15-axes_pos(1)+legend_pos(1) 5-axes_pos(2)+legend_pos(2) legend_pos(3) legend_pos(4)] );
     
    end
     
    %adjusts the new figure accordingly
    set(newFig,'Units',axes_units);
    set(newFig,'Position',[15 5 axes_pos(3)+30 axes_pos(4)+10]);
     
    %saves the plot
    saveas(newFig,fullfile(pathname, filename)) 
     
    %closes the figure
    close(newFig)

Now, run the GUI to check that the pop-up menu and save plot functionality work. Specifically, lets try to select the “Phase” option on the pop-up menu. This is what you should get (click on figure to enlarge):

Data Processing GUI

Pages: 1 2 3 4 5 6 7