How to create column headers?

24 ビュー (過去 30 日間)
Hannah
Hannah 2021 年 8 月 13 日
コメント済み: Hannah 2021 年 8 月 16 日
I have the following code:
months=1:12;
orientation = [0, -90];
row = 1:3;
tilt = [0,10,20,30,40,50,60,70,80,90];
irr = [10,110,120,130,140,150,160,170,180,190];
table.x=tilt;
table.y=irr;
for m=1:length(months)
for o=1:length(orientation)
for r=1:length(row)
disp([m,o,r])
for n=1:length(tilt)
table.x=tilt(n);
table.y=irr(n);
disp(table)
end
end
end
end
How can I have x and y as the column headers instaed of having them noting every element? for example for m=1,0=2,r=3, I want the output to be:
1 2 3
x y
0 10
10 110
20 120
30 130
40 140
50 150
60 160
70 170
80 180
90 190

採用された回答

Scott MacKenzie
Scott MacKenzie 2021 年 8 月 13 日
編集済み: Scott MacKenzie 2021 年 8 月 13 日
Something like this will work. I'm only showing the inner for-loop.
fprintf('%6s%6s\n\n', 'x', 'y');
for n=1:length(tilt)
table.x=tilt(n);
table.y=irr(n);
disp([table.x table.y]);
end
Output (last table only):
12 2 3
x y
0 10
10 110
20 120
30 130
40 140
50 150
60 160
70 170
  2 件のコメント
Hannah
Hannah 2021 年 8 月 13 日
That's perfect! thank you so much :)
Scott MacKenzie
Scott MacKenzie 2021 年 8 月 13 日
You're welcome. Glad to help. Good luck.

サインインしてコメントする。

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2021 年 8 月 13 日
My preference is to use the table function to create a MATLAB table (data type table instead of structure)
x = [0,10,20,30,40,50,60,70,80,90]';
y = [10,110,120,130,140,150,160,170,180,190]';
data = table(x,y)
data = 10×2 table
x y __ ___ 0 10 10 110 20 120 30 130 40 140 50 150 60 160 70 170 80 180 90 190
However, to stick to the same approach you are using, remove the last loop. I've reduced months and row to limit the output length here.
months=1:1;
orientation = [0, -90];
row = 1:1;
tilt = [0,10,20,30,40,50,60,70,80,90];
irr = [10,110,120,130,140,150,160,170,180,190];
for m=1:length(months)
for o=1:length(orientation)
for r=1:length(row)
disp([m,o,r])
table.x=tilt';
table.y=irr';
disp(["x","y"])
disp([table.x table.y])
end
end
end
1 1 1
"x" "y"
0 10 10 110 20 120 30 130 40 140 50 150 60 160 70 170 80 180 90 190
1 2 1
"x" "y"
0 10 10 110 20 120 30 130 40 140 50 150 60 160 70 170 80 180 90 190
To create exactly what you specified, you can use fprintf instead of disp
for m=1:length(months)
for o=1:length(orientation)
for r=1:length(row)
fprintf('%g %g %g',m,o,r)
fprintf('%s %s',"x","y")
table.x=tilt';
table.y=irr';
fprintf('%g %g\n',[table.x table.y]')
end
end
end
1 1 1
x y
0 10 10 110 20 120 30 130 40 140 50 150 60 160 70 170 80 180 90 190
1 2 1
x y
0 10 10 110 20 120 30 130 40 140 50 150 60 160 70 170 80 180 90 190
  1 件のコメント
Hannah
Hannah 2021 年 8 月 16 日
Yes that's great too. makes it nicer. Thank you so much!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by