File Exchange

image thumbnail


version (3.75 KB) by Michelle Hirsch
Write a MATLAB figure to a Microsoft Excel spreadsheet.


Updated 04 May 2021

From GitHub

View Version History

View license on GitHub

Editor's Note: This file was selected as MATLAB Central Pick of the Week

XLSWRITEFIG allows you to programmatically place a MATLAB figure inside of an Excel spreadsheet. This can be a useful supplement to XLSWRITE.

Cite As

Michelle Hirsch (2021). xlswritefig (, GitHub. Retrieved .

Comments and Ratings (35)


Michelle Hirsch

Thanks for reporting this issue. I've fixed it, using copygraphics instead of hgexport for use in 20a or newer.


I get the error: hgexport(hFig,'-clipboard') when I try to write a histogram (with horizontal bars) to an excel file. (I provided a reproducible example on your github site here:

Michelle Hirsch

You are right, Jakob. This has always been Windows only - I'm not sure how it ended up listing the other platforms. I'll try to figure out how to update the submission.

Jakob Weis

hgexport(FILENAME,'-clipboard') is only supported on Windows. This function is not compatible with Mac or Linux. Not sure why those are mentioned as compatible platforms.

Michelle Hirsch

@Adila could you share some of your code to help me diagnose the issue? Please email me through the contact link on my MATLAB Central profile page.

Adila Hoque

ok so update: I was able to write it into an excel spreadsheet, but I can't seem to be able to save it to the right one. The figure opens in a new spreadsheet and not the one I assigned it to. I'd appreciate any help! Thanks

jinang patel

Edit: solved.. format used was xls which was different that xlsx of original file

Fan Wang

Alexandru Bitca

When you have a 2D plot with a large number of data points it causes an internal error in MATLAB on Windows OS.

Michelle Hirsch

Sorry for the issues you are having, Svenja. Unfortunately I don't have access to a Windows machine anymore so I can't debug this.

Svenja Borgmann

I mean 2D plots and 3D plots

Svenja Borgmann

Works fine for 2D figures so far, but unfortunately not for 3D figures: doesn't print figure in Excel sheet, get's stuck executing, so that the only solution is to quit Matlab with the Task Manager

Svenja Borgmann

Michelle Hirsch

@XAXRXTX could you provide more specific reproduction steps? The function is designed to work one figure at a time, so if you make two separate calls it should work. If it doesn't, there's something I'd need to fix.


I got an error when trying to insert two figures !


asif iqbal

Michelle Hirsch

Thanks, Kurt! If you are comfortable doing so, would you mind making a pull request with these changes on the GitHub repo? (Just follow the "Learn More" link). It's OK if not, I'm just being lazy :)

Kurt Patzik

In orderto be able to write to a file located in a different directory, I use
% Check if the input variable filename is a "full filename" (=including path)
% * if filename contains '\' we assume that this is a full filename
% * else we use the present working directory as path
if sum(strfind(filename, filesep)) == 0
% filename
Path2Excel = [pwd filesep];
ExcelFullFilename = [Path2Excel filename];
ExcelFullFilename = filename;

In the rest of the code I replace [pwd filesep filename] with ExcelFullFilename


I am running a loop, so the set of calcs are iterated producing number of plots, then I am using the xlswritefig to export plots to Excel. Is there way to set the range rather than a cell where the figures are saved for each consecutive plot. At the moment, if the iteration produces more than one plot, they would export on each other in Excel. Thank you



@Hirsch, thank you for this function and your response. Best regards.

Michelle Hirsch

@Ayhan - sorry, but this function doesn't offer any ability to modify figures that have already been put into an Excel file.


is there a way to refresh the figure?
because it appends figures to excel file now.
i would like to change old figure to new one.

Lowell Toms

Oddly, if I pause for a bit:

hFig = figure(1);

It works fine. Thanks for the file!

Lowell Toms

Using Win7 with Excel 2013, I get no errors, but the output is just a plot outline with no graphics. Hoping you'll update this - or tell me what I'm doing wrong when I run your example - because it's just what I was looking for.


Dustyn Roberts

How can I adapt this code to write to a file that's already open? For example, if I have this line that works great:

xlswrite(filename, percentError, 1, 'C4');

and I want to put a figure next to the data, I'd like to write:

xlswritefig(gcf, filename, 'Sheet1', 'E2')

Which seems like it should work but I get a long error:

Error using Interface.000208DB_0000_0000_C000_000000000046/invoke
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: 'C:\Users\User\Documents\MATLAB\C:\Users\User\Dropbox\MATLAB\002 Human
Machine\v6_1\results_20131102_222147.xlsx' could not be found. Check the spelling of the file name, and verify that the
file location is correct.

If you are trying to open the file from your list of most recently used files, make sure that the file has not been
renamed, moved, or deleted.
Help File: xlmain11.chm
Help Context ID: 0

Error in xlswritefig (line 79)
op = invoke(Excel.Workbooks, 'open', [pwd filesep filename]);

Error in calcResults_v6_1 (line 226)
xlswritefig(gcf, filename, 'Sheet1', 'E2')

Any thoughts?

Krishna Kumar

Krishna Kumar

Peter Lindberg

I changed line 80 to:

op = invoke(Excel.Workbooks, 'open', [filename]);

before it read:

op = invoke(Excel.Workbooks, 'open', [pwd filesep filename]);

but pwd filesep assumes you have the xl file in the same catalogue as your program. Just a thought.


Hello Scott,

Thanks for the M-File. Its very helpful.

I had few queries:
1. Can we resize the figure in Excel automatically.
2. I am running a loop, so the same set of instructions are iterated. Is there any way by which we could advance the cell where the figure has to be saved.

Any help is appreciated.


Peter Lindberg

Peter Lindberg

I've added a "add a new worksheet function":

WorkSheets = Excel.sheets;
message = struct('message',{''},'identifier',{''});

Sheets = Excel.ActiveWorkBook.Sheets;
target_sheet = get(Sheets, 'Item', sheetname);
catch %#ok<CTCH> Suppress so that this function works in releases without MException
% Error if the sheet doesn't exist. It would be nice to create it, but
% I'm too lazy.
% The alternative to try/catch is to call xlsfinfo to see if the sheet exists, but
% that's really slow.
disp(['Sheet ' sheetname ' does not exist!']);
Sheet = invoke(WorkSheets,'Add') = sheetname
target_sheet = Sheet

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!