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

29 ビュー (過去 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 件のコメント
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 件)

Community Treasure Hunt

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

Start Hunting!

Translated by