How to divide results into 3 columns?

2 views (last 30 days)
Hannah
Hannah on 18 Aug 2021
Commented: Image Analyst on 18 Aug 2021
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 Comments
Hannah
Hannah on 18 Aug 2021
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

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 18 Aug 2021
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 Comments
Image Analyst
Image Analyst on 18 Aug 2021
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);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!