Displaying Tabular Data with heading in matlab

36 ビュー (過去 30 日間)
Chuzymatics Chuzymatics
Chuzymatics Chuzymatics 2015 年 1 月 25 日
回答済み: Image Analyst 2015 年 1 月 25 日
Dear All, I intend to display result of my calculation in a way that each column will have a heading in the command window. I want x values neatly displayed under x-val; y values under y-val etc; thanks if you can help. Here is my code:
x =1:20;
y= x.^2;
z = x*20;
table =[x; y; z];
disp('Table shows table values')
disp('x-val, y-val, z-val')
fprintf('%8.2f %10.1f %12.2f\n', table)

採用された回答

Image Analyst
Image Analyst 2015 年 1 月 25 日
Try this:
fprintf(' x-val, y-val, z-val\n')
fprintf('--------------------------------\n')
fprintf('%8.2f %10.1f %12.2f\n', table)

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2015 年 1 月 25 日
Chuzymatics - depending upon your version of MATLAB (I think from R2013b and above) why not try and use the table function? Something like the following may work
T = table(x',y',z','VariableNames',{'xval','yval','zval'})
which creates a table as
T =
xval yval zval
____ ____ ____
1 1 20
2 4 40
3 9 60
4 16 80
5 25 100
6 36 120
7 49 140
8 64 160
9 81 180
10 100 200
11 121 220
12 144 240
13 169 260
14 196 280
15 225 300
16 256 320
17 289 340
18 324 360
19 361 380
20 400 400
Note that the inputs to table are columns (that is why there is a transpose for each input variable) and the names of each variable (or columns names) is specified in the array of strings (note that these names must be valid MATLAB variable names, that is why I removed the dash from each).
Try the above and see what happens!
  2 件のコメント
Chuzymatics Chuzymatics
Chuzymatics Chuzymatics 2015 年 1 月 25 日
Many thanks. My version of MATLAB is 2007 so the table function is not there. However, I edited and ran my code and got the error:
??? Error using ==> subsindex
Function 'subsindex' is not defined for values of class 'cell'.
Error in ==> fmattOpt at 9
T = table(x',y',z','VariableNames',{'xval','yval','zval'})
Image Analyst
Image Analyst 2015 年 1 月 25 日
Why does your code include Geoff's table() function which you admit you don't have since you have an antique version of MATLAB? You can't use table like that. Your table is a simple numerical matrix.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by