How to label columns in matrix?

How can I give the columns contaning values for m, o, r ,t and Diff_irr a label?
ResultMtx = [];
for m=1:length(months) %loop 12 times for the 12 months
for o=1:length(orientation) %loop 2 times for south and east
for r = 1:length(row) %loop 3 times for the 3 rows
for t=1:length(tilt) %loop 10 times for the 10 tilting possibilities, this also represents the rows in the matric of irr
%_____________calculations here to find Diff_irr_____________
ResultMtx = [ResultMtx;
m o r t Diff_irr];
end
ResultMtx = [ResultMtx;
nan nan nan nan nan];
end
end
end
xlswrite(strcat(Result_File), ResultMtx);
I attempted to write:
ResultMtx = [ResultMtx;
['month%d, orientation%d, rows%d, tilt%d Irradiance%d,', m, o, r, t, Diff_irr]];
but that did't work obviously and I got the error:
"Dimensions of arrays being concatenated are not consistent."

3 Comments

try using table
month = ResultMtx(:,1);
orientation = ResultMtx(:,2);
rows = ResultMtx(:,3);
tilt = ResultMtx(:,4);
Irradiance = ResultMtx(:,5);
ResultTable = table(month, orientation, rows, tilt, Irradiance);
regards
Hi Marcel, I did that and at the end I wrote:
writetable(ResultTable, strcat(Result_File))
I got an error. Do you know what I'm doing wrong?
Use the following code after calculating the 'Diff_irr':
temp = [m o r t Diff_irr];
ResultMtx = [ResultMtx; temp];

Sign in to comment.

Answers (1)

% After the loop, you obtain ResultMtx. Convert it into a table with
% column names. Then write the table
ResultMtx = array2table(ResultMtx, 'VariableNames', ["Month","orientation", "rows", "tilt", "Irradiance"]);
writetable(T, 'res.xlsx');

3 Comments

Thanks for the reply. in the loop, I wrote:
ResultMtx = [ResultMtx;
m, o, r, t, Diff_irr];
end
ResultMtx = [ResultMtx;
nan nan nan nan nan];
ResultMtx = array2table(ResultMtx, 'VariableNames', ["Month","orientation", "rows", "tilt", "Irradiance"]);
outside the loop I wrote:
writetable(T, Result_File);
winopen(Result_File)
but i get the error:
"All input arguments must be tables."
The following line should be outside of the loop:
ResultMtx = array2table(ResultMtx, 'VariableNames', ["Month","orientation", "rows", "tilt", "Irradiance"]);
Now it's a lot closer to what I want (see image below).
But how can I have the column headers after every 10 lines, i.e. where I highlighted in yellow?

Sign in to comment.

Categories

Asked:

on 18 Aug 2021

Commented:

on 18 Aug 2021

Community Treasure Hunt

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

Start Hunting!