Displaying Tabular Data with heading in matlab

33 views (last 30 days)
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)

Accepted Answer

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

More Answers (1)

Geoff Hayes
Geoff Hayes on 25 Jan 2015
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 Comments
Chuzymatics Chuzymatics
Chuzymatics Chuzymatics on 25 Jan 2015
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 on 25 Jan 2015
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.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!