Matlab GUI Tutorial - Basic Data Processing Tool
26 Nov 2007 Quan Quach 23 comments 24891 views
Exporting the Data to Excel
Exporting data to a medium that users can manipulate is very desirable. Since many people work with Excel, adding a feature to export data to Excel can come in quite handy. Lets see how we can export data to Excel. Add the following code to export_pushbutton_Callback:
%if the data hasn't been processed yet, %nothing happens when this button is pressed if (handles.processDataCompleted == 0) return end saveDataToExcel(handles.data,handles.legendData);
Add this code as its own separate m-file, or add it to the data-processing-tool.m file.
function saveDataToExcel(data, fileNames) %stores savepath for the phase plot [filename, pathname] = uiputfile({'*.xls','Excel (*.xls)'},'Save Data to Excel File','default'); %it is assumed that the frequency range is the same for all the data sets magnitudeData = [data{1}(:,1)]; phaseData = [data{1}(:,1)]; for x = 1:length(fileNames) magnitudeData = [magnitudeData data{x}(:,2)]; phaseData = [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,phaseData,'Magnitude Data','A3'); xlswrite(saveFileName,['Data file: ' fileNames],'Phase Data','A1'); xlswrite(saveFileName,['Frequency (GHz)' phaseDegrees],'Phase Data','A2'); xlswrite(saveFileName,phaseData,'Phase Data','A3'); deleteEmptyExcelSheets(saveFileName);
And to erase those empty sheets when the Excel file is created, we can use the following function that is discussed in this post.
function deleteEmptyExcelSheets(fileName) %this function erases any empty sheets in an excel document %the input fileName is the entire path of the file %for example, fileName = 'C:\Documents and Settings\matlab\myExcelFile.xls' excelObj = actxserver('Excel.Application'); %opens up an excel object excelWorkbook = excelObj.workbooks.Open(fileName); worksheets = excelObj.sheets; %total number of sheets in workbook numSheets = worksheets.Count; count=1; for x=1:numSheets %stores the current number of sheets in the workbook %this number will change if sheets are deleted temp = worksheets.count; %if there's only one sheet left, we must leave it or else %there will be an error. if (temp == 1) break; end %this command will only delete the sheet if it is empty worksheets.Item(count).Delete; %if a sheet was not deleted, we move on to the next one %by incrementing the count variable if (temp == worksheets.count) count = count + 1; end end excelWorkbook.Save; excelWorkbook.Close(false); excelObj.Quit; delete(excelObj);
Once again, you should try testing the GUI to make sure it works, and to test the exporting capabilties. There are many different ways you can export your data to excel. For instance, you can export a separate Excel file for each input file, or you can lump all the magnitude data together into one file, and lump all the phase data into another file. The function that is used in this example does the latter.
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’??