how to write a matrix to text file with loop name of .txt
13 views (last 30 days)
Show older comments
Let's say, I have a code like this:
for i = 1:size(cell,2)
data = cell{i}; %kx3 matrix
dlmwrite('name_change_acoording_to_each_loop.txt', data, 'delimiter','\t','newline','pc','precision',15);
end
I want to write a matrix to .txt file. And The result will be a set of .txt file, with the name is according to the value of i
Example:
cell={[1 1 1] [1 2 3;3 2 1] [4 5 6;7 8 9;1 1 1] [2 2 2;3 3 3]};
-Loop 1: i=1 ---> the result of text file is : 1.txt
-Loop 2: i=2 ---> the result of text file is : 2.txt
-Loop 3: i=3 ---> the result of text file is : 3.txt
-Loop 4: i=4 ---> the result of text file is : 4.txt
Finally, I will have 4 text files with the name is: 1.txt ; 2.txt ; 3.txt; 4.txt
But my code doesn't provide the result as my expectation.
How can I do that? How can I fix my code? Thanks
0 Comments
Accepted Answer
madhan ravi
on 14 May 2019
sprintf('%d.txt',i) % this would be your filename inside the loop
2 Comments
madhan ravi
on 14 May 2019
for i = 1:size(cell,2)
data = cell{i}; %kx3 matrix
filename=sprintf('%d.txt',i); % this would be your filename inside the loop
dlmwrite(filename, data, 'delimiter','\t','newline','pc','precision',15);
end
More Answers (0)
See Also
Categories
Find more on Cell Arrays 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!