xlswrite: write values in different rows in each iteration in Excel file

26 views (last 30 days)
hi, i need doing xlswrite: write values in different rows starting from A2,A3... An, if assume A1 for the title each coulmn
eaxmple : in Excel file the storing values as following :
Row1 sim_seconds' ,'simtickx'
Row 2 223 33
row3 12 55
i attached data sets and the code :
clc;
clear all;
xlfiles = dir('*.xlsx'); % You are in the folder of xl files
Nfiles = length(xlfiles) ; % number of xl files
% loop for each file
for i = 1:Nfiles
fname = xlfiles(i).name ; % file name
[~,~,dat] = xlsread(fname) ; % read the file
%%do what you want %%%
p= strcmp(dat(:,1),'sim_ticks') ;
a(i)= cell2mat(dat(p,2));
p1= strcmp(dat(:,1),'sim_insts') ;
b(i) = cell2mat(dat(p1,2));
data= [ a (i), b(i)];
xlswrite('A.xlsx',data,'Sheet1','A2'); %Write data
end
col_header={'sim_seconds','simtickx'}; %Row cell array (for column labels)
xlswrite('A.xlsx',col_header,'Sheet1','A1'); %Write column header
how can put rows sequence A1, A2, A3 ... Ai , changing based on iteraion value as example : xlswrite('A.xlsx',data,'Sheet1','A( i) ');

Accepted Answer

Image Analyst
Image Analyst on 25 Nov 2019
Try this
cellReference = sprintf('A%d', i);
xlswrite('A.xlsx', data, 'Sheet1', cellReference);
or better yet just store in a cell array and call just xlswrite just once:
INSIDE the loop, stuff values into the ith row of the cell array:
ca{i, 1} = a(i);
ca{i, 2} = b(i);
AFTER the loop, call xlswrite():
cellReference = sprintf('A2');
xlswrite('A.xlsx', ca, 'Sheet1', cellReference); % Write out ca, not data.

More Answers (0)

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!