How to convert MATLAB plots to xls (Excel) sheets ?

I have almost ten to fifteen MATLAB figures which I've got by running a MATLAB code. The code takes hours to plot a figure. My professor has now asked me to make .xls (Excel) sheets from the data associated with these figures. Although, I know how to do it and that is by using the "xlswrite" command, but, really ?, I have to run the code for hours again? Can anybody recoomend me an easy way using which I could get data files directly from the MATLAB figures ?

 Accepted Answer

You can access data of a plot with the line handle. For instance :
% Current figure handle (or use openfig if you saved the figure in a FIG-File)
hFigure = gcf;
% Find line handle
hLines = findobj(gcf, 'Type', 'Line')
% Here are your data
hLines.XData
hLines.YData
In this example, I assume you have only one line in each plot.
Since R2019a, prefer writetable, writematrix, or writecell instead of xlswrite

11 Comments

Thanks, So I got the data down in the command window. You mean, I should now copy it to the Excell sheet ?
Assign it to a variable, then export it in Excel sheet.
% Matrix to export
mXY = hLines.XData, hLines.YData;
% Write into file
writematrix(mXY, 'YourWorkbook.xlsx', 'Sheet', 'YourSheetName')
Thanks Arthur for your reply, the problem is that is that I have 3 or sometimes 4 like (array) in one figure :/
Can you share an example of a figure to export (I mean the saved FIG-File) ?
Dear Arthur, Here you can have an example ! I would like to export the data of all the three pulses against the same time axis. I obtained these three pulses by running the code three times. Means one run, one pulse.
You can use handle as arrays and DisplayName property to get the signals names (what you display in the legend).
% Current figure handle (or use openfig if you saved the figure in a FIG-File)
hFigure = gcf;
% Find lines handles
vhLines = findobj(gcf, 'Type', 'Line')
% X data, I assume it's the same for all lines (otherwise use interp1)
vX = vhLines(1).XData;
% All Y data
mY = [vhLines.YData];
% All names
cNames = {vhLines.DisplayName};
% Cell to export
cToExport = {
['t [s]', cNames]; % Headers
num2cell([vX, mY])}
% Write into file
writecell(cToExport, 'YourWorkbook.xlsx', 'Sheet', 'YourSheetName')
I didn't test the code, cToExport should be in one cell :
% Cell to export
cToExport = [
't [s]', cNames; % Headers
num2cell([vX, mY])]
And you are in R2017a, use xlswrite instead of write cell
I used the xlswrite but they are now saying that the input array is empty. I attached the ".fig" file. Can you a try and then exchange the ".m" file ?
Here you go
% Current figure handle (or use openfig if you saved the figure in a FIG-File)
hFigure = gcf;
% Find lines handles
vhLines = findobj(gcf, 'Type', 'Line');
% X data, I assume it's the same for all lines (otherwise use interp1)
vX = vhLines(1).XData;
% All Y data
mY = vertcat(vhLines.YData)';
% All names
cNames = {vhLines.DisplayName};
% Cell to export
cToExport = [
't [s]', cNames; % Headers
num2cell([vX', mY])];
% Write into file
xlswrite('YourWorkbook.xlsx', cToExport, 'YourSheetName')
Dear Arthur, Thanks alot. It worked :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!