How to divide results into 3 columns?
3 次查看(过去 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): ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/715022/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/715022/image.png)
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
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.
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!