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

14 vues (au cours des 30 derniers jours)
ha ha
ha ha le 14 Mai 2019
Commenté : madhan ravi le 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

Réponse acceptée

madhan ravi
madhan ravi le 14 Mai 2019
sprintf('%d.txt',i) % this would be your filename inside the loop
  2 commentaires
ha ha
ha ha le 14 Mai 2019
Modifié(e) : ha ha le 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 le 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by