writing output in excel file with variable to decide starting cell to write data

5 views (last 30 days)
Hi,
I have done 100 test runs and for each run I have processed and averaged data(single value) for 20 different signals. I have to run matlab code to process the data
after each code run I get a set of 20 values for that particular test run. I wanted to write them in single excel file.
Currently I am able to write xlswrite command to output test run data of particular test (whichever I am running matlab code for) in excel format but if I run code next time again it will overlap my previous data in excel file
Can someone explain me how can I use xlswrite command with a variable
for example
this is the command I use to have excel output
xlswrite (filename,Output,sheetname,'B2');
if there is way to assign variable (instead of 'B2' which is a constant) to decide where to start printing data in excel , I believe that will resolve my querry.

Accepted Answer

dpb
dpb on 19 Mar 2019
Edited: dpb on 19 Mar 2019
Sure, just use my little utility xlsAddr to build the address and pass to xlswrite
for i=1:N
output=yourfunction(data(:,i));
xlswrite('Output.xlsx',output,xlsAddr(2,i+1)) % write to column B2, C2, ...
end
function rnge=xlsAddr(row,col)
% Build Excel cell address from row, column
%
% RNGE=XLSADDR(ROW,COL) will return an Excel cell address
% formed from the input ROW,COL values. Either input may be
% string or numeric and will be converted to canonical form
if isnumeric(col)
if ~isscalar(col), error('Input Column Not Scalar'), end
rnge=num2str('A'+[fix(col/26) rem(col,26)]-1,'$%c%c');
rnge(rnge=='@')=[]; % cleanup for single character
else
rnge=['$' col];
end
if isnumeric(row)
if ~isscalar(row), error('Input Row Not Scalar'), end
rnge=[rnge num2str(row,'$%d')];
else
row=num2str(row,'$%d');
if ~all(ismember(row,'0':'9')), error('Invalid Excel Address: Row not numeric'), end
rnge=[rnge row];
end
I'm not sure, later releases of Matlab may have similar functions; at the time I wrote this there weren't any supplied excepting in some private supporting routines...
Alternatively, save your data into an array by similarly allocating room for each new vector in a column of an array and then wait and write the whole shebang at once. Unless you have zillions of these, memory should be no issue.

More Answers (0)

Tags

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!