Creating text file with columns of data

20 ビュー (過去 30 日間)
g
g 2018 年 11 月 27 日
編集済み: lb_net 2020 年 8 月 25 日
How do I create a text file with two columns of text correspond to columns of data?
I currently have
data = [a b];
% a and b are columns
fileID = fopen('textfile.txt','w');
fprintf(fileID,'%f %f\n',data);
fclose(fileID);
This creates two columns in textfile.txt, however, all of the "a" data is listed before the "b" data, instead of in 2 columns with the separate data. How do I get the proper data in each respective column of the text file? Thanks

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2018 年 11 月 27 日
In this case, you can use
fprintf(fileID,'%f %f\n',data')
  1 件のコメント
lb_net
lb_net 2020 年 8 月 25 日
編集済み: lb_net 2020 年 8 月 25 日
Do you have an idea if I can also save additional values in the text file?
a = [1;2;3;4;5];
b = [1;2;3;4;5];
data = [a b];
fileID = fopen('textfile.txt','w');
for i = 1:3
fprintf(fileID,' %d %f %f\n',i, data');
end
fclose(fileID);
Here is a picture of how the final result file should look like:

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

カテゴリ

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