fprintf table

84 ビュー (過去 30 日間)
TheSpaceGuy
TheSpaceGuy 2011 年 10 月 22 日
コメント済み: Walter Roberson 2023 年 4 月 6 日
Hi guys! I am having trouble making this table using only fprintf commands (NO LOOPS allowed). This is my code for the data that is supposed to be in the table
t = [40 35 30 25 20 15 10 5 0 -5 -10 -15 -20 -25 -30 -35 -40 -45];
v = [5 10 15 20 25 30 35 40 45 50 55 60]';
[T,V] = meshgrid(t,v);
Tc = 35.74 + 0.6215*T - 35.75*(V.^.16) + 0.4275*T .* (V.^.16)
Tchill = round(Tc)
All the help I can get will be appreciated!

採用された回答

Walter Roberson
Walter Roberson 2011 年 10 月 22 日
The secret to fprintf() to print tables, is to construct a single array in which all of the numeric values appear row by row in the same order they would appear on the screen -- and then pass the transpose of that table as the fprintf() argument after the format string.
  2 件のコメント
TheSpaceGuy
TheSpaceGuy 2011 年 10 月 23 日
Thanks for the tip!
Walter Roberson
Walter Roberson 2023 年 4 月 6 日
My previous answer was written in 2011.
These days, I would probably instead use something like
fprintf(FID, '%s\n', join(compose("%4d", Tchill)))
The key difference here is that while fprintf() and sprintf() go "down" columns, compose() is happy to go across rows.
Tchill = [1 2; 3 4]
Tchill = 2×2
1 2 3 4
fprintf('%4d %4d\n', Tchill)
1 3 2 4
fprintf('%s\n', join(compose("%4d", Tchill)))
1 2 3 4
The fprintf() version output the items in linear index order; the compose() version ran across rows.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by