Matlab to Excel via Activex - How to iteratively add Matlab variables?
19 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
10B
el 18 de Sept. de 2015
Comentada: 10B
el 28 de Sept. de 2015
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.
0 comentarios
Respuesta aceptada
Kirby Fears
el 18 de Sept. de 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.
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 comentarios
Kirby Fears
el 21 de Sept. de 2015
Editada: Kirby Fears
el 21 de Sept. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!