Print a table with 4 columns fprintf

4 Ansichten (letzte 30 Tage)
Sook Yee Lim
Sook Yee Lim am 31 Mär. 2017
Bearbeitet: Stephen23 am 31 Mär. 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

Antworten (2)

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

Melden Sie sich an, um zu kommentieren.


Stephen23
Stephen23 am 31 Mär. 2017
Bearbeitet: Stephen23 am 31 Mär. 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'.

Kategorien

Mehr zu Elementary Math finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by