How to divide results into 3 columns?

1 次查看(过去 30 天)
I have generated the following excel file using Matlab (see file attached). As you can see, I have 72 different records, separated by an empty line. How can I program my script so that I will have three columns: column 1 would be for records that have row=1, column 2 would be for records that have row=2, and column 3 would be for records that have row=3. I did it manually to demonstrate what I want (see image below):
This is the code I used:
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
%************Calculate Diff_irr here*************
ResultMtx = [ResultMtx;
m, o, r, t, Diff_irr];
end
ResultMtx = [ResultMtx;
nan nan nan nan nan];
end
end
end
ResultMtx = array2table(ResultMtx, 'VariableNames', ["Month","orientation", "rows", "tilt", "Irradiance"]);
writetable(ResultMtx, Result_File);
  2 个评论
Yazan
Yazan 2021-8-18
Do you mean that you need to group the table by Month and create nested tables? One table for Month =1, one for Month = 2, etc.
Hannah
Hannah 2021-8-18
hi Yazan,
no I want to group them by row. So as shown in my image, the first column is for r=1, second os for r=2, and third is r=3

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2021-8-18
Did you try
data = readmatrix('results.xlsx')
% Extract rows that have a 1 in column 1:
rowsWith1 = data(:, 1) == 1;
m1 = data(rowsWith1, :);
% Extract rows that have a 2 in column 1:
rowsWith2 = data(:, 1) == 2;
m2 = data(rowsWith2, :);
% Extract rows that have a 3 in column 1:
rowsWith3 = data(:, 1) == 3;
m3 = data(rowsWith3, :);
That will give double matrices.
If you want a table instead of a matrix you can do this:
data = readtable('results.xlsx')
% Extract rows that have a 1 in column 1:
rowsWith1 = data{:, 1} == 1;
t1 = data(rowsWith1, :);
% Extract rows that have a 2 in column 1:
rowsWith2 = data{:, 1} == 2;
t2 = data(rowsWith2, :);
% Extract rows that have a 3 in column 1:
rowsWith3 = data{:, 1} == 3;
t3 = data(rowsWith3, :);
  2 个评论
Hannah
Hannah 2021-8-18
Thanks for the help. So I wrote:
ResultMtx = array2table(ResultMtx, 'VariableNames', ["Month","orientation", "rows", "tilt", "Irradiance"]);
writetable(ResultMtx);
data = readtable(Result_File);
% Extract rows that have a 1 in column 1:
rowsWith1 = data{:, 1} == 1;
t1 = data(rowsWith1, :);
% Extract rows that have a 2 in column 1:
rowsWith2 = data{:, 1} == 2;
t2 = data(rowsWith2, :);
% Extract rows that have a 3 in column 1:
rowsWith3 = data{:, 1} == 3;
t3 = data(rowsWith3, :);
writetable(t1,t2,t3, Result_File);
and it's not working. What am I doing wrong?
Image Analyst
Image Analyst 2021-8-18
writetable cannot write 3 tables as once. Check the documentation. You need to combine them before writing
t123 = [t1;t2;t3];
writetable(t123, Result_File);

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by