Writing data in columns from "for loop" to ".txt" file
26 ビュー (過去 30 日間)
古いコメントを表示
Hello!
I need to write data in multiple columns (column-wise) to a text file. I used the following code in which the data is written to a cell array.
j = 0;
c = {};
for i=1:10;
b = [i; i+1; i+2; i+3];
j = j+1;
c{j} = {b};
end
disp(c)
result_fid = fopen('result_testing.txt','w+');
% fprintf(result_fid,'%d\r\n',c{3}{1}); % for one column
for count=1:10 % for multiple columns
fprintf(result_fid,'%d\r\n',c{count}{1});
end
fclose(result_fid);
The task is done when only one column is to be written to the output .txt file. However, in case of multiple columns, it writes all the data in only one column. How can i get the data i.e., "b" on multiple columns in .txt file? i.e., for i=1:10 i get 10 columns.
0 件のコメント
採用された回答
Walter Roberson
2020 年 10 月 19 日
The only way to write column-by-column to a text file is to arrange the code so that at each step, it reads the existing contents of the lines and writes out the extended lines to a new file.
It has been decades since MATLAB was supported on an operating system that supported appending to an existing line without rewriting the entire file -- and when MATLAB was supported on that operating system, MATLAB used the I/O subsystem that did not support that option. So hypothetically decades ago MATLAB could have supported doing that, on that one operating system, but did not. In all other cases, it has been literally impossible on the operating systems MATLAB has ever run on.
All file systems ever supported by MS Windows, and all filesystems ever supported by Apple, or Unix, treat files as a stream of bytes, with particular characters in the stream being used to mark the end of lines. Historically MS Windows used to use Carriage Return followed by Linefeed (\r\n) as the end of line characters, and old enough versions of Apple's file system used separation between paragraphs instead of between lines; all modern versions of MS Windows and Apple and Linux use linefeed as the line seperator.
None of the supported operating system has any inherent support for "lines" . No support for inserting lines or deleting lines, or positioning directly to a line, or appending more to a line. The only support those operating systems have is for positioning by byte offset, and for reading or writing bytes (and for truncating files). Adding more to an existing line in all of those operating systems requires re-writing the entire rest of the file.
TL;DR: Don't do that.
Transpose your data so that the known data at any time becomes a row -- and then adding more content just becomes a matter of adding more rows to the file.
Or keep everything in memory and write it all out at the end.
Or read in the file and write out the entire new content, such as by using readmatrix() or readcell() and writematrix() or writecell() -- this will be inefficient and is not recommended, but at least it will take care of the overhead so you do not need to handle it yourself.
If the problem is just that you have multiple columns in memory and you do not know how to write them all out at one time, then we can work with you on that. For example in a case like your example you would use cell2mat() or horzcat() to create a rectangular numeric matrix that could be written out all at one time. Formats can be constructed dynamically:
ncol = size(DataToWrite, 2);
fmt = [repmat('%d ', 1, ncol-1), '%d\r\n'];
fprintf(fid, fmt, DataToWrite.'); %transpose is important!
その他の回答 (1 件)
Sudheer Bhimireddy
2020 年 10 月 19 日
Your fprintf syntax is what writing everything in one column. Try the below line and see:
for count=1:10
fprintf(result_fid,'%d %d %d %d\r\n',c{count}{1});
end
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!