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

38 ビュー (過去 30 日間)
10B
10B 2015 年 9 月 18 日
コメント済み: 10B 2015 年 9 月 28 日
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.

採用された回答

Kirby Fears
Kirby Fears 2015 年 9 月 18 日
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 件のコメント
Kirby Fears
Kirby Fears 2015 年 9 月 21 日
編集済み: Kirby Fears 2015 年 9 月 21 日
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 2015 年 9 月 28 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by