how to write a matrix to text file with loop name of .txt

3 Ansichten (letzte 30 Tage)
ha ha
ha ha am 14 Mai 2019
Kommentiert: madhan ravi am 14 Mai 2019
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

Akzeptierte Antwort

madhan ravi
madhan ravi am 14 Mai 2019
sprintf('%d.txt',i) % this would be your filename inside the loop
  2 Kommentare
ha ha
ha ha am 14 Mai 2019
Bearbeitet: ha ha am 14 Mai 2019
You mean my modified 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);
sprintf('%d.txt',i) % this would be your filename inside the loop
end
madhan ravi
madhan ravi am 14 Mai 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by