How can save cell array (numbers and string ) using xlswrite ?

1 次查看(过去 30 天)
hi, i have this code, i tried to store the cell arry (Bandwidth1) in excel file inside iteraion , but the excel file results empty vaule !
lfiles = 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
p2= strcmp(dat(:,1),'sim_seconds') ;
simsecond (i)= cell2mat(dat(p2,2));
dcachmi= strcmp(dat(:,1),'system.cpu00.dcache.overall_misses::total') ;
dcachemi00 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu01.dcache.overall_misses::total') ;
dcachemi01 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu02.dcache.overall_misses::total') ;
dcachemi02 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu03.dcache.overall_misses::total') ;
dcachemi03 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu04.dcache.overall_misses::total') ;
dcachemi04 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu05.dcache.overall_misses::total') ;
dcachemi05 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu06.dcache.overall_misses::total') ;
dcachemi06 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu07.dcache.overall_misses::total') ;
dcachemi07 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu08.dcache.overall_misses::total') ;
dcachemi08 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu09.dcache.overall_misses::total') ;
dcachemi09 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu10.dcache.overall_misses::total') ;
dcachemi10 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu11.dcache.overall_misses::total') ;
dcachemi11 = cell2mat(dat(dcachmi,2));
dcachmi= strcmp(dat(:,1),'system.cpu12.dcache.overall_misses::total') ;
dcachemi12 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu13.dcache.overall_misses::total') ;
dcachemi13 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu14.dcache.overall_misses::total') ;
dcachemi14 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu15.dcache.overall_misses::total') ;
dcachemi15 = cell2mat(dat(dcachmi,2)) ;
dcachemitotal(i)=dcachemi15+dcachemi14+dcachemi13+dcachemi12+dcachemi11+dcachemi10+dcachemi09+dcachemi08+dcachemi07+dcachemi06+dcachemi05+dcachemi04+dcachemi03+dcachemi02+dcachemi01+dcachemi00;
Bw1(i)= (dcachemitotal(i)*64);
Bw(i)= Bw1(i) ./simsecond (i);
Bandwidth1= sizeconvert ([Bw(i)]) ;
%%%%%%%%%%%%% this is my question %%%%%%%%%%%%%%%%
ca{i, 1} = Bandwidth1; % how can save this array in the Excel file ?
end
col_header={'Bandwidthsize'};
xlswrite('B.xlsx',col_header,'Sheet1','A1');
cellReference = sprintf('A2');
xlswrite('B.xlsx', ca, 'Sheet1', cellReference);
  2 个评论
Stephen23
Stephen23 2019-11-26
编辑:Stephen23 2019-11-26
Using numbered variables is a sign that you are doing something wrong.
Copy-and-pasting code is a sign that you are doing something wrong.
Rather than this copy-and-paste fifteen times:
dcachmi= strcmp(dat(:,1),'system.cpu00.dcache.overall_misses::total') ;
dcachemi00 = cell2mat(dat(dcachmi,2)) ;
dcachmi= strcmp(dat(:,1),'system.cpu01.dcache.overall_misses::total') ;
dcachemi01 = cell2mat(dat(dcachmi,2)) ;
...
you should use a loop with:
  • sprintf to generate those character vectors, and
  • indexing to store the result in one vector or matrix,
then this incredible line of code:
dcachemitotal(i)=dcachemi15+dcachemi14+dcachemi13+dcachemi12+dcachemi11+dcachemi10+dcachemi09+dcachemi08+dcachemi07+dcachemi06+dcachemi05+dcachemi04+dcachemi03+dcachemi02+dcachemi01+dcachemi00;
becomes one simple sum operation.

请先登录,再进行评论。

回答(1 个)

Furat Alobaidy
Furat Alobaidy 2019-11-26
编辑:Furat Alobaidy 2019-11-26
thanks for your notice , i update the code based own your comments, but still not able to write all the values for the cell array ( Bandwidth1) just last value in the table !, i tried with writable but its still same problem,
how can store all values for the variable (Bandwidth1) for the loop in excel file?
xlfiles = dir('*.xlsx');
Nfiles = length(xlfiles) ;
for i = 1:Nfiles
fname = xlfiles(i).name ;
[~,~,dat] = xlsread(fname) ;
p2= strcmp(dat(:,1),'sim_seconds') ;
simsecond (i)= cell2mat(dat(p2,2));
dcachmi= strcmp(dat(:,1),'system.cpu00.dcache.overall_misses::total') ;
dcachemi00 = cell2mat(dat(dcachmi,2)) ;
dcachemitotal(i)=dcachemi00;
Bw(i)= (dcachemitotal(i)*64)./simsecond (i);
Bandwidth1= sizeconvert ([Bw(i)]) ;
cellReference = sprintf('A1');
xlswrite( 'B.xlsx',Bandwidth1,'Sheet1',cellReference);
end

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by