How to divide results into 3 columns?

조회 수: 1 (최근 30일)
Hannah
Hannah 2021년 8월 18일
댓글: Image Analyst 2021년 8월 18일
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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by