save txt file in loop

7 vues (au cours des 30 derniers jours)
tony kevine
tony kevine le 11 Avr 2015
Commenté : tony kevine le 12 Avr 2015
a1=magic(2);
a2=magic(3);
a3=magic(4);
if i assume a1 a2 a3 and i want to save them to txt file Individually how to write it in loop .
like this a1.txt a2.txt a3.txt
i really need help because i have 50s need to save .it too huge.!!
for i=1:3 save end
  1 commentaire
Stephen23
Stephen23 le 11 Avr 2015
Modifié(e) : Stephen23 le 12 Avr 2015
Whatever you do, do not try to create fifty dynamically named variables! This is a bad programming practice, and should be avoided.
Here is an explanation of why dynamically named variables is a poor programming practice:

Connectez-vous pour commenter.

Réponses (3)

Jan
Jan le 11 Avr 2015
Avoid hiding an index in the name of the variables. Then the solution is easy:
a = cell(1, 3);
a{1} = magic(2);
a{2} = magic(3);
a{3} = magic(4);
for ia = 1:numel(a)
save(sprintf('File%d.mat'm ia), a{ia});
end
  1 commentaire
tony kevine
tony kevine le 12 Avr 2015
tks!!

Connectez-vous pour commenter.


Chris McComb
Chris McComb le 11 Avr 2015
You could do something like this using the dlmwrite function:
for i=1:1:50
dlmwrite(sprintf('%d.txt', i), magic(i));
end
This will save the first 50 magic matrices to text files named 1.txt, 2.txt, 3.txt, etc.
  1 commentaire
tony kevine
tony kevine le 12 Avr 2015
tks!!

Connectez-vous pour commenter.


tony kevine
tony kevine le 12 Avr 2015
thanks so much!!

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by