Short way xlswrite code
2 views (last 30 days)
Show older comments
I guys, I've written a matlab code and at the end of the code I want to generate an excel file. So I wrote the code in this way, but I'd like to write the same code in a short way (that is, without repeating the part "filename"). How can I do that?
filename = sprintf('EP3');
for f=1:N
sheetname = sprintf('%d °C',Temp(f)-273.15);
xlswrite(filename,{'xV'},sheetname,'A1');
xlswrite(filename,{'Vent ratio'},sheetname,'B1');
xlswrite(filename,{'Conversion'},sheetname,'C1');
xlswrite(filename,{'Splitting ratio'},sheetname,'D1');
xlswrite(filename,{'F1'},sheetname,'E1');
xlswrite(filename,{'F2'},sheetname,'F1');
xlswrite(filename,{'R'},sheetname,'G1');
xlswrite(filename,{'T'},sheetname,'H1');
xlswrite(filename,{'V'},sheetname,'I1');
xlswrite(filename,{'F toluene'},sheetname,'J1');
xlswrite(filename,{'F H2'},sheetname,'K1');
xlswrite(filename,{'F CH4'},sheetname,'L1');
xlswrite(filename,{'[-]'},sheetname,'A2:D2');
xlswrite(filename,{'[kmol/h]'},sheetname,'E2:L2');
xlswrite(filename,xV_vector',sheetname,'A3');
xlswrite(filename,(V_matrix(f,:)./(R_matrix(f,:)+V_matrix(f,:)))',sheetname,'B1');
xlswrite(filename,X_matrix(f,:)',sheetname,'C1');
xlswrite(filename,(R_matrix(f,:)./(R_matrix(f,:)+V_matrix(f,:)))',sheetname,'D1');
xlswrite(filename,F1_matrix(f,:)',sheetname,'E3');
xlswrite(filename,F2_matrix(f,:)',sheetname,'F3');
xlswrite(filename,R_matrix(f,:)',sheetname,'G1');
xlswrite(filename,T_matrix(f,:)',sheetname,'H1');
xlswrite(filename,V_matrix(f,:)',sheetname,'I1');
xlswrite(filename,{'F toluene'},sheetname,'J1');
xlswrite(filename,{'F H2'},sheetname,'K1');
xlswrite(filename,{'F CH4'},sheetname,'L1');
end
2 Comments
Shubham Gupta
on 5 Nov 2019
Why don't you first create cell vectors and then use xlswrite only one time at the end? For e.g.
filename = sprintf('EP3');
VarName = {'xV','Vent ratio',.. .. .. .. , 'F CH4'}; %1x12 cell vector containing Names
Units{1x12} = '';
Units(1:4) = {'[-]'};
Units(5:end) = {'[kmol/h]'};
%% same with the other lines too;
%% Then you make final cell vector to write in excel
for f = 1:N
sheetname = sprintf('%d °C',Temp(f)-273.15);
%% Write code to value cell vector
finalCell = [VarName;Units;Values];
xlswrite(filename,finalCell,sheetname,'A1'); % using only one time per loop
end
Finally, what I didn't understand from your code is:
xlswrite(filename,{'Conversion'},sheetname,'C1'); % first you write this
xlswrite(filename,X_matrix(f,:)',sheetname,'C1'); % then you change it with this in the same loop?
% I was thinking it should be 'C3', is it a typo?
Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!