save the variables whose names are always changing over different loops

Consider the example below. The aim is , in every circulation, to generate a variable first, only a1 in the fist circulation for example,then save the generated variable into hardware, then the workspace should be cleared right before each circulation ends considering the huge size of the data. What I can do now is only to generate the desired variable in the workspace, and I am at a loss on how to save it to the hard disk automatically .
for i=1:10;
name=['a',num2str(i)];
eval([name,'=',num2str(i^2),';']);
%-------
% PLEASE help add a line or more here to save ai into hard disk
%-------
clear all
clc
end
Thanks a million

 Réponse acceptée

If you are saving each value to disk as a separate file, the only thing you need to change is the filename. You can reuse the same variable in the loop. Does the format of the file matter? If not I recommend saving as a MAT-file as shown below:
for i = 1:10
a = i^2;
save(['myfile',num2str(i)],'a');
end

5 commentaires

Thanks a million!!!!!!
That is brilliant!!!!!
magic parentheses
very very bad idea that makes me cry, first you are writing to the hard drive every iteration and second you make a file for every iteration!!
I would use the computer memory to save data from all iterations in MEMORY and after all iterations are done I would save that data to the hard drive, simple fast and reliable.
Sorry for making you suffer, but I have no other choice. I agree with you, in that way it is easier and faster, but probably only true for data with a small size. In my case, the size of data can reach hundreds of Mb each. If I do in the usual way, Matlab will cry. It is not good for achieving the goal.
ok with large data it starts to make some sense
Thanks for starting understanding Haha

Connectez-vous pour commenter.

Plus de réponses (1)

Don't do this!!!

2 commentaires

EVAL and CLEAR ALL in one example... It is worth to avoid both.
Good suggestions. Thank you very much!!!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by