strings to text file using num2str,strcat, and fprintf

15 ビュー (過去 30 日間)
Seb
Seb 2015 年 11 月 21 日
回答済み: Walter Roberson 2015 年 11 月 22 日
I have been attempting to print data in vertical columns to a text file using the below format. I have tried about 30 different ways to get the formatting right, and am failing miserably. Any suggestion or help will be appreciated. I am fairly new at Matlab, and this was the explained procedure to get things to format correctly. I will be using real data and similar character arrays when it's all said and done.
A=['tr';'tr';'tr';'tr';'tr';'tr'];
B=randn([6 1]);
C=randn([6 1]);
D=randn([6 1]);
E=randn([6 1]);
F=randn([6 1]);
G=randn([6 1]);
H=randn([6 1]);
I=randn([6 1]);
D=strcat(A,num2str(B),num2str(C),num2str(D),num2str(E),num2str(F),num2str(G),num2str(H),num2str(I));
format='%s\r\n';
fileID=fopen('bs.txt','w');
fprintf(fileID,format,D')

採用された回答

Walter Roberson
Walter Roberson 2015 年 11 月 22 日
N = 6;
A = repmat({'tr'}, N, 1);
B = randn(N, 1);
C = randn(N, 1);
D = randn(N, 1);
E = randn(N, 1);
F = randn(N, 1);
G = randn(N, 1);
H = randn(N, 1);
I = randn(N, 1);
B_I_cell = num2cell([B,C,D,E,F,G,H,I]);
A_I_cell = [A,B_I_cell] .';
fmt = ['%s', repmat(' %7.4f', 1, 8), '\n'];
fileID = fopen('bs.txt','w');
fprintf(fileID, fmt, A_I_cell{:});
fclose(fileID);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by