%%% What XLSWRITE is doing under the hood, plus more
%% PARAMETERS (if you make this a function, these should be inputs)
file = [pwd '\XLSWRITEtest.xls'];
%% OPEN EXCEL APPLICATION
h = actxserver('Excel.Application');
% Show the Excel window
set(h, 'Visible', 1);
% What attributes and operations are available for the handle "h"? These
% are attributes and operations of the Excel object ... and are documented
% in the Excel Visual Basic Reference:
% (In Microsoft Office 2003 Excel)
% Help -> Microsoft Excel Help -> Table of Contents -> Microsoft Excel
% Visual Basic Reference
%% INSERT NEW WORKBOOK
W = h.Workbooks.Add;
%% WORKBOOKS CONTAIN WORKSHEETS
Sheets = h.ActiveWorkBook.Sheets;
% Add a fourth sheet (by default, a workbook contains
% three worksheets - add a new one before [], after #3)
Sheets.Add( [], Sheets.Item(3) );
%% ADD DATA AND CHARTS
for j = 1:4
%% Rename
Sheets.Item(j).Name = ['s' int2str(j)];
%% Make it "Active"
Sheets.Item(j).Activate;
Activesheet = h.Activesheet;
%% Insert (random) data
A = floor(256*rand(10,1));
ActivesheetRange = get(Activesheet,'Range','A1:A10');
set(ActivesheetRange, 'Value', A);
%% Add a ChartObject (container for embedded charts)
co = Activesheet.ChartObjects.Add(150,10,300,300); %(Left,Top,Width,Height)
co.Chart.ChartType = 'xlLine';
co.Chart.SeriesCollection.Add(ActivesheetRange);
%% Chart Formatting
% Legend off
co.Chart.Legend.Delete;
% White Background, Border off
co.Chart.PlotArea.Interior.ColorIndex = 0;
co.Chart.PlotArea.Border.LineStyle = 0;
% Major Gridlines off (x-axis is 1, y-axis is 2)
co.Chart.Axes(2).HasMajorGridlines = 0;
% Tick Marks inside
co.Chart.Axes(1).MajorTickMark = 'xlTickMarkInside';
co.Chart.Axes(2).MajorTickmark = 'xlTickMarkInside';
% x-axis label
co.Chart.Axes(1).HasTitle = 1;
co.Chart.Axes(1).AxisTitle.Text = 'x-axis';
co.Chart.Axes(2).HasTitle = 2;
co.Chart.Axes(2).AxisTitle.Text = 'y-axis';
% Chart Title
co.Chart.HasTitle = 1;
co.Chart.ChartTitle.Text = ['Chart in s' int2str(j)];
end
%% SAVE WORKBOOK
% *Full path name should be used*
invoke( W, 'SaveAs', file );
%% CLEAN UP
% Terminate the server session to which the handle is atttached
invoke( h, 'Quit' );
% Release all interfaces derived from the server
delete(h)