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

4 visualizaciones (últimos 30 días)
ha ha
ha ha el 14 de Mayo de 2019
Comentada: madhan ravi el 14 de Mayo de 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

Respuesta aceptada

madhan ravi
madhan ravi el 14 de Mayo de 2019
sprintf('%d.txt',i) % this would be your filename inside the loop
  2 comentarios
ha ha
ha ha el 14 de Mayo de 2019
Editada: ha ha el 14 de Mayo de 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 el 14 de Mayo de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by