Writing data in columns from "for loop" to ".txt" file

21 ビュー (過去 30 日間)
Nasir Mehmood
Nasir Mehmood 2020 年 10 月 19 日
コメント済み: Nasir Mehmood 2020 年 10 月 19 日
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.

採用された回答

Walter Roberson
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 件のコメント
Nasir Mehmood
Nasir Mehmood 2020 年 10 月 19 日
Thanks for your kind reply! In the light of your explanation and valuable suggestions I modified the code to the following form:
j = 0;
c = {};
for i=1:10;
b = [i; i+1; i+2; i+3];
j = j+1;
c{j} = {b};
end
arr = cat(2,c{:}); % concatenate the cell arrays
fin = cell2mat(arr); % convert the cell array into matrix
dlmwrite('result_testing.txt',[fin],'delimiter', '\t');
and finally get the desired output:
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
Thanks a lot!

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

その他の回答 (1 件)

Sudheer Bhimireddy
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
  1 件のコメント
Nasir Mehmood
Nasir Mehmood 2020 年 10 月 19 日
Thanks for reply. This does not work for my purpose it gives "b" in the form of row. The output has four columns (= number of elements of "b").
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
6 7 8 9
7 8 9 10
8 9 10 11
9 10 11 12
10 11 12 13
However, I need "b" as it is i.e., in the form of column. (seperate column for every iteration). Like this:
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13

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

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by