Print a table with 4 columns fprintf

13 views (last 30 days)
Sook Yee Lim
Sook Yee Lim on 31 Mar 2017
Edited: Stephen23 on 31 Mar 2017
95.0000 1.8700 10.9831 8.9874
2.0000 45.0000 1.6000 7.1402
3.0000 76.0000 1.7000 9.3224
4.0000 88.0000 1.6100 9.5381
5.0000 55.0000 1.5000 7.4204
6.0000 50.0000 1.6100 7.5010
7.0000 61.0000 1.5800 8.0519
8.0000 105.0000 1.8300 11.2821
I specify the vectors like this: N=[1,2,3,4,5,6,7,8]; and I use fprintf(fileID,'%5.2f\n\' but if i repeat this for my other variable it all ends up in one column?How to get it in 4 columns

Answers (2)

KSSV
KSSV on 31 Mar 2017
A = magic(5) ;
fprintf([repmat('%f\t', 1, size(A, 2)) '\n'], A')
  1 Comment
Stephen23
Stephen23 on 31 Mar 2017
Note that transpose A.' should be used, and not conjugate transpose A'.

Sign in to comment.


Stephen23
Stephen23 on 31 Mar 2017
Edited: Stephen23 on 31 Mar 2017
When you put all of the data into one matrix then your task is easy:
>> M = [95, 1, 10.9831, 8.9874;...
2, 45, 1.6000, 7.1402;...
3, 76, 1.7000, 9.3224;...
4, 88, 1.6100, 9.5381;...
5, 55, 1.5000, 7.4204;...
6, 50, 1.6100, 7.5010;...
7, 61, 1.5800, 8.0519;...
8, 105, 1.8300, 11.2821];
>> fmt = '%3d,%4d,%6.2f,%6.2f\n';
>> fprintf(fmt,M.')
95, 1, 10.98, 8.99
2, 45, 1.60, 7.14
3, 76, 1.70, 9.32
4, 88, 1.61, 9.54
5, 55, 1.50, 7.42
6, 50, 1.61, 7.50
7, 61, 1.58, 8.05
8, 105, 1.83, 11.28
Note that I specified different formats for each column, and used transpose M.' instead of the linear algebra operation ctranspose M'.

Categories

Find more on Elementary Math in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!