fprintf command issue. tricky columns.

1 Ansicht (letzte 30 Tage)
Burners
Burners am 1 Okt. 2012
Kommentiert: Sara Pierson am 1 Apr. 2021
a=1:5;
b=90:94;
fprintf('Variables A : %.f | Variables B : %.f\n',a,b);
Consider the script above.
Variable a are the small values. I want to print all the variables on the LEFT COLUMN. Then variable b on the RIGHT column.
However with this sequence of fprintf the variable a results in an entirely different order.
1 2
3 4
5 90
91 92
93 94

Akzeptierte Antwort

Honglei Chen
Honglei Chen am 1 Okt. 2012
Bearbeitet: Honglei Chen am 1 Okt. 2012
The trick is [a;b], like this
fprintf('Variables A : %.f | Variables B : %.f\n',[a;b]);
  1 Kommentar
Sara Pierson
Sara Pierson am 1 Apr. 2021
THIS WORKED FOR ME! Thank you! (I am using matlab 2020b btw if that helps anyone else)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Burners
Burners am 1 Okt. 2012
What i want is, the same style as my vectors are arranged
1 90
2 91
3 92
4 93
5 94
  6 Kommentare
Walter Roberson
Walter Roberson am 7 Sep. 2019
fprintf(fileID,formatSpec,A1,...,An) applies the formatSpec to all elements of arrays A1,...An in column order, and writes the data to a text file.
If you have R2016b or later you might also want to look at compose(), which does not have this same behaviour.
Rik
Rik am 7 Sep. 2019
The fundamental point is that Matlab arrays are column-based. We might read row by row when we look at an array to be written, but Matlab doesn't. Maybe there should be a warning to this effect in the doc, but as far as I'm aware there isn't. If you want to avoid ambiguous situations, you should provide each input as a separate array.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by