xlswrite: write values in different rows in each iteration in Excel file
22 次查看(过去 30 天)
显示 更早的评论
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) ');
0 个评论
采纳的回答
Image Analyst
2019-11-25
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!