Writing data in columns from "for loop" to ".txt" file
Show older comments
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.
Accepted Answer
More Answers (1)
Sudheer Bhimireddy
on 19 Oct 2020
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 Comment
Nasir Mehmood
on 19 Oct 2020
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!