while writing txt file variables/data are shown in one column instead of being different column

8 ビュー (過去 30 日間)
Ismita
Ismita 2022 年 4 月 17 日
編集済み: Voss 2022 年 4 月 17 日
Dear all,
While writing a txt file of dataset, though in work space I see output like L(as I need) but in the txt file, it shows like output of P. My code is also attached.
b = [1,2,3,4];
c = [3,2,4,5];
L = [b c] output(L) = [1,2,3,4, 3,2,4,5]
P = [b;c] output(P) = [1,2,3,4]
[3,2,4,5]
"Code:.
..
.
File = [Y, time_sec, B, GSE, BGSE, VxSE, VySE, VzSE, Density, Temp];
fileID = fopen('Alldata.txt','w');
nbytes = fprintf(fileID,'%12.5f %18.8f %12.5f %12.5f %12.5f %12.5f %12.5f %12.5f %12.5f %12.5f \r\n',File);
fclose(fileID)"

回答 (1 件)

Voss
Voss 2022 年 4 月 17 日
When you fprintf a matrix, MATLAB prints the elements in order of: first column first, followed by the second column, and so on:
M = magic(3)
M = 3×3
8 1 6 3 5 7 4 9 2
% M(1,1) M(2,1) M(3,1) \n
% M(1,2) M(2,2) M(3,2) \n
% M(1,3) M(2,3) M(3,3) \n
fprintf('%d %d %d\n',M)
8 3 4 1 5 9 6 7 2
To get the elements printed in order of first row first, then second row, etc., transpose the matrix:
% M(1,1) M(1,2) M(1,3) \n
% M(2,1) M(2,2) M(2,3) \n
% M(3,1) M(3,2) M(3,3) \n
fprintf('%d %d %d\n',M.')
8 1 6 3 5 7 4 9 2
  2 件のコメント
Voss
Voss 2022 年 4 月 17 日
編集済み: Voss 2022 年 4 月 17 日
You're welcome! Let me know if this solves the problem completely, or if you're still not getting the expected output.

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

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by