Matlab GUI Tutorial - Basic Data Processing Tool
26 Nov 2007 Quan Quach 23 comments 24892 views
Introduction
This tutorial outlines the basic skeleton for a data processing GUI. The tutorial provides you with the visual aspect of the GUI via download, and then goes on to provide the code to each component callback. This tutorial draws upon many of the basic GUI elements: adding files to a listbox, parsing data, plotting data onto the GUI, saving GUI plots, disabling/enabling buttons, exporting data to Excel format, and many other things.
If you just want to see the finished product, you can download the source files here. The source files include the GUI files, the m-files for the sub functions, and 3 sample data files. You can play around with this GUI to learn more about how GUIs are designed and coded. You can also use this GUI as a foundation for developing more advanced GUIs. I highly encourage that you take the time to look through the source code, and to hack it to your content. Feel free to modify it so that it suits your purposes.
This tutorial is written for those with a good amount of experience creating a Matlab GUI. If you’re new to creating GUIs in Matlab, you should visit this tutorial first. Basic/Advanced knowledge of Matlab is highly recommended. Matlab version 2007a is used in writing this tutorial. Both earlier versions and new versions should be compatible as well (as long as it isan’t too outdated). Let’s get started!
23 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.
Quan Quach
Wonderful tutorial! I’m learning alot from you excellent code and style.
I found as I was working through the export to Excel portion of you code that the magnitude was being output to both the magnitude and phase sheets. Scratching my head, I put a breakpoint in and found two places where the code needed to be changed to get the phase numbers output. I’ve copied your code below with the corrections and noted what the change is with a comment. I suspect that you put these “errors” in to see if people were really paying attention.
for x = 1:length(fileNames)
magnitudeData = [magnitudeData data{x}(:,2)];
phaseData = [phaseData data{x}(:,3)]; % ([phaseData data{x}(:,2)];)
magDB{x} = ‘Mag (dB)’;
phaseDegrees{x} = ‘Phase(Degrees)’;
end
saveFileName = fullfile(pathname, filename);
xlswrite(saveFileName,['Data File' fileNames],’Magnitude Data’,'A1′);
xlswrite(saveFileName,['Frequency (GHz)' magDB],’Magnitude Data’,'A2′);
xlswrite(saveFileName,magnitudeData,’Magnitude Data’,'A3′);
%Was (xlswrite(saveFileName,phaseData,’Magnitude Data’,'A3′);
Intended only to help and to demonstrate that I really am learning from your Tutorial.
Thanks again,
Dave
Great catch Dave! With all this code flying around I’m bound to make silly mistakes.
Quan
ya Great job……….thanks
but can u provide me the MATLAB source code for Penrose Tiling………
its very urgent……..plz help……..
guys,
i’m using GUI matlab for my project
i’ve created 2 editbox (user input) + 1 staticbox (sum) as interface, and a ‘calculate’ button..
i wanted to write a function in ‘calculate_callback’ so that the 2 editbox will add together and give result to the ’sum_box’.
what should i write under the ‘calculate_callback’??