-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxlswritefig.m
143 lines (121 loc) · 3.7 KB
/
xlswritefig.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function xlswritefig(hFig,filename,sheetname,xlcell)
% XLSWRITEFIG Write a MATLAB figure to an Excel spreadsheet
%
% xlswritefig(hFig,filename,sheetname,xlcell)
%
% All inputs are optional:
%
% hFig: Handle to MATLAB figure. If empty, current figure is
% exported
% filename (string) Name of Excel file, including extension. If not specified, contents will
% be opened in a new Excel spreadsheet.
% sheetname: Name of sheet to write data to. The default is 'Sheet1'
% If specified, a sheet with the specified name must
% exist
% xlcell: Designation of cell to indicate the upper-left corner of
% the figure (e.g. 'D2'). Default = 'A1'
%
% Requirements: Must have Microsoft Excel installed. Microsoft Windows
% only.
%
% Ex:
% Paste the current figure into a new Excel spreadsheet which is left open.
% plot(rand(10,1))
% drawnow % Maybe overkill, but ensures plot is drawn first
% xlswritefig
%
% Specify all options.
% hFig = figure;
% surf(peaks)
% xlswritefig(hFig,'MyNewFile.xlsx','Sheet2','D4')
% winopen('MyNewFile.xlsx')
% Michelle Hirsch
% The MathWorks
%
% Is this function useful? Drop me a line to let me know!
if nargin==0 || isempty(hFig)
hFig = gcf;
end
if nargin<2 || isempty(filename)
filename ='';
dontsave = true;
else
dontsave = false;
% Create full file name with path
filename = fullfilename(filename);
end
if nargin < 3 || isempty(sheetname)
sheetname = 'Sheet1';
end;
if nargin<4
xlcell = 'A1';
end
% Put figure in clipboard
if ~verLessThan('matlab','9.8')
warning off MATLAB:print:ExportExcludesUI
copygraphics(hFig)
warning on MATLAB:print:ExportExcludesUI
else
% For older releases, use hgexport. Set renderer to painters to make
% sure it looks right.
r = get(hFig,'Renderer');
set(hFig,'Renderer','Painters')
drawnow
hgexport(hFig,'-clipboard')
set(hFig,'Renderer',r)
end
% Open Excel, add workbook, change active worksheet,
% get/put array, save.
% First, open an Excel Server.
Excel = actxserver('Excel.Application');
% Two cases:
% * Open a new workbook, save with given file name
% * Open an existing workbook
if exist(filename,'file')==0
% The following case if file does not exist (Creating New File)
op = invoke(Excel.Workbooks,'Add');
% invoke(op, 'SaveAs', [pwd filesep filename]);
new=1;
else
% The following case if file does exist (Opening File)
% disp(['Opening Excel File ...(' filename ')']);
op = invoke(Excel.Workbooks, 'open', filename);
new=0;
end
% set(Excel, 'Visible', 0);
% Make the specified sheet active.
try
Sheets = Excel.ActiveWorkBook.Sheets;
target_sheet = get(Sheets, 'Item', sheetname);
catch %#ok<CTCH> Suppress so that this function works in releases without MException
% Add the sheet if it doesn't exist
target_sheet = Excel.ActiveWorkBook.Worksheets.Add();
target_sheet.Name = sheetname;
end;
invoke(target_sheet, 'Activate');
Activesheet = Excel.Activesheet;
% Paste to specified cell
Paste(Activesheet,get(Activesheet,'Range',xlcell,xlcell))
% Save and clean up
if new && ~dontsave
invoke(op, 'SaveAs', filename);
elseif ~new
invoke(op, 'Save');
else % New, but don't save
set(Excel, 'Visible', 1);
return % Bail out before quitting Excel
end
invoke(Excel, 'Quit');
delete(Excel)
end
function filename = fullfilename(filename)
[filepath, filename, fileext] = fileparts(filename);
if isempty(filepath)
filepath = pwd;
end
if isempty(fileext)
fileext = '.xlsx';
end
filename = fullfile(filepath, [filename fileext]);
end