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) ');

채택된 답변

Image Analyst
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 CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by