Matlab GUI Tutorial - Basic Data Processing Tool
26 Nov 2007 Quan Quach 19 comments 20865 views
Pop-up Menu and Save Plot Callbacks
-
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.
-
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):
19 Responses to “Matlab GUI Tutorial - Basic Data Processing Tool”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Hi!
Thanks for a very clear tutorial. A lot of things I’ve learnt from this tutorial. Really appreciate it.
TQ,
zuri
I just want to say thanks a bunch!!! All your tutorials will help me and others like me gain knowledge and confidence in our jobs and lives. I’ll be sure to “pay it forward” your generosity.
Hello,
I’ve been working my way through these tutorials the last few days and they are excellent - far superior to the MATLAB manual on GUIs!
A question I have regarding the current tutorial. Is there a way to change the colours of the lines in the plots - ie a different colour for each testfile that is plotted.
Hello Jack,
I’m glad you found this tutorial useful.
You can change plot line colors using the following method
You should check the documentation on the plot command to get more details on this.
Quan
Hello Quan,
Wow what a quick reply - impressive!
Yes I have tried putting this command in. What I would like to do is have a different colour for each testfile rather than have them the same colour. In that way is it easier to identify the runs.
So TESTFILEA - Blue.
TESTFILEB - Red
TESTFILEC - Green
It is straight forward to do this in MATLAB with the raw data but it would be nice to do it in the GUI.
I have never used GUI before but following your tutorials I have gained far more information than the detailed but not very instructive MATLAB manual.
One way to do this is to define a cell array as the following:
Then, if you use a for loop to generate your plot, you can do the following:
Not sure if this is the best way, I’ll have to get back to you on this one.
If you’re using a for loop to cycle through data that you want to plot, and you want the curves to be different colors. You should use
instead of
holdHi Quan,
thanks for the help on uigetfile.
One thing that is still not working for me is that after i obtain the file path, I can’t use it. ie i save the file path as myFile, (it saves myFile as ‘C:\ etc’), but then when i try and run xlsread like this: m = xlsread(myFile, ’sheet’, ‘range’), it tells me that the filename needs to be a string. I thought it was already a string!
What am I doing wrong?
A separate unrelated question, each time i load my figure, I get the following error:
??? Error using ==> feval
Undefined command/function ‘inputFiles_edit_CreateFcn’.
Error in ==> gui_mainfcn at 75
feval(varargin{:});
Error in ==> ClassificationModelOne at 45
gui_mainfcn(gui_State, varargin{:});
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
I don’t have a function called: inputFiles_edit_CreateFcn. I did at one point, but I have since removed it from the m file and from the design of the GUI. Is it hiding somewhere else that i don’t know about?
Hi Kate,
I have been scouting forums looking for an answer to the same question you posed above (2nd question). I have an error that seems to be the exact same as the one you described above in relation to a function ‘txt_CreateFcn.’ I have been developing this GUI for a few weeks so the only thing I can imagine that happened was that I accidentally typed in that name incorrectly at some stage for a text box and deleted it just as quickly. The problem seems to be that the GUI still looks for the CreateFcn for the deleted object. I inserted a ‘txt_CreatFcn’ at the end of my m file and the error disappeared.
Hope this helps,
Best of luck with the GUI.
Steve.
Hello,
I’ve a problem with the function savePlotWithinGUI when I save the plot with the extension *.fig. I can save the figure without problems, but when I’m going to open it, Matlab give me the next error:
Error using ==>open
Output argument “valid” (and maybe others) not assigned during call to “C\Archivos de programa\MATLAB71\toolbox\matlab\datatypes\isprop.m (isprop)”.
If I don’t save the legend, there ins’t any problem. But I need save the figure with the legend and extension *.fig.
I hope that you can help me.
Thanks.
Hi Isabel,
I just tried to recreate your error but I couldn’t. I saved the figure WITH the legend and it was able to open up fine on it’s on. I’m sorry I cannot help you here.
Quan
Thanks for your answer Quan.
Can you save the plot with the legend in a file *.fig? I’m continue with the same error, I can’t use twice the function copyobj, maybe, we are working with differents version of Matlab. I’m working with matlab 7.1.
Thanks
Hi Isabel,
I’m working with version 2007a. That might be the problem
Quan
Is there a way to recall the directory where the files were selected from if the working directory is elsewhere?
Thanks
Thank you for this tutorial.
Thank youuuuuuuuuuuuuuuuuuuuuuuuuuuuu!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Quan, do you know how to make a Java wrapper for a GUI and put it on the web? I’ve seen that you need the Matlab compiler for Java (https://tagteamdbserver.mathworks.com/ttserverroot/Download/47137_91126v02_MATLAB_Build_JA.pdf), but I was wondering if there’s a way without having to buy the compiler.
Thank you very much!
Ana.
Halo Quan Quach
Wow amazingggg, it is very helpful for me in learning GUI……..You are such generous person in sharing your knowledge with others……
Btw I still have question concerning data processing. My boss asked me to do an animation of measurement data with matlab. I do it in simple way, just write it in Mfile. To make it more interactive, I decided to make it in GUI. But there is still one element that is not exist in your explanation. In my my animation I want to make a pause button, so someone can stop the animation for a while, and can continue again after pressing the button. I would be very thankful if you could give me a hint how to do that.
Thank you very much for your kindly help
Stefanus
Thank you for the tutorial. This example seems rather hard for me. If it is possible to publish the data process in seperate lessons again, like each .m file as a different class, it would be very nice.