Matlab to Excel via Activex - How to iteratively add Matlab variables?

27 views (last 30 days)
Hello Community,
I need some help with 'Housekeeping' following the running of a Matlab script which produces a series of variables that I need to store externally (Excel). The script will be run several times, so the variable contents will change each time the script is run. What I want to do is sequentially add new data to an excel workbook each time the script runs. From elsewhere on the forum, I have some ActiveX code that opens the workbook and adds the 'titles' I want to the w.book:
% First open an Excel Server
Excel = actxserver('Excel.Application');
set(Excel, 'Visible', 1);
% Insert a new workbook
Workbooks = Excel.Workbooks;
Workbook = invoke(Workbooks, 'Add');
% Make the first sheet active
Sheets = Excel.ActiveWorkBook.Sheets;
sheet1 = get(Sheets, 'Item', 1);
invoke(sheet1, 'Activate');
% Get a handle to the active sheet
Activesheet = Excel.Activesheet;
%set(ActivesheetRange, 'Value', A);
ActivesheetRange = get(Activesheet,'Range','A1:G1');
set(ActivesheetRange, 'Value', xlsxcol); %xlsxcol is a variable from the workspace containing 7 'titles'
Now the next step is to add the data from the other variables, beneath the titles in the appropriate place. eg:
title1...title2...etc.
var1......var2...etc.
Then on the next run of the script, I would want new variables to be written beneath the previous variables, eg:
title1...title2...etc.
var1......var2...etc.
var1......var2...etc.
I feel that by having to use this:
ActivesheetRange = get(Activesheet,'Range','A1:G1');
I am predetermining the range that I put the data in which is problematic for repeats of say 300 iterations! I know I need a 'for' loop - but am uncertain how to do this with ActiveX as well.
So could you help with the next step that I am missing here?
Thanks for your time.
10B.

Accepted Answer

Kirby Fears
Kirby Fears on 18 Sep 2015
Hi 10B,
If you want to output 300 separate tables, stacking them vertically in a single excel sheet will probably be hard to use later.
You could write each table to a separate excel sheet by using the optional sheet argument of xlswrite.
Alternatively you can write each table to a separate csv file using writetable.
Writing to csv files will be much faster, the files will take up less space on your disk, and they are generally easier to use later.
  4 Comments
Kirby Fears
Kirby Fears on 21 Sep 2015
Edited: Kirby Fears on 21 Sep 2015
10B,
You can iteratively manipulate the Excel range to step down the worksheet. Is the number of columns fixed for each table? How about the number of rows?
Below is a simple example assuming each table is the same size. If the columns or rows are not fixed, it can be generalized easily enough.
% Initializing fixed values
ntab=300;
startcol='A';
endcol='G';
startrow=2;
nrows=10;
xlsxcol=arrayfun(@(d)['col' num2str(d)],1:7,'UniformOutput',false);
% First open an Excel Server
Excel = actxserver('Excel.Application');
set(Excel, 'Visible', 1);
% Insert a new workbook
Workbooks = Excel.Workbooks;
Workbook = invoke(Workbooks, 'Add');
% Make the first sheet active
Sheets = Excel.ActiveWorkBook.Sheets;
sheet1 = get(Sheets, 'Item', 1);
invoke(sheet1, 'Activate');
% Get a handle to the active sheet
Activesheet = Excel.Activesheet;
% Write headers
ActivesheetRange = get(Activesheet,'Range','A1:G1');
set(ActivesheetRange, 'Value', xlsxcol);
% After opening the workbook and writing headers:
for j=1:ntab,
% Calculate table J before printing
tableout=j*ones(nrows,7); % This is only a sample
% Write table to active Excel sheet
excelrange=[startcol num2str((j-1)*nrows+startrow) ':' ...
endcol num2str(j*nrows+startrow-1)];
ActivesheetRange = get(Activesheet,'Range',excelrange);
set(ActivesheetRange, 'Value', tableout);
end
% Save and close workbook
% Close Excel
Hope this helps.
10B
10B on 28 Sep 2015
Hello Kirkby,
Sorry, for some reason I did not get a notification for this answer. I will give this a run and get back to you shortly.
Regards,
10B.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!