save the variables whose names are always changing over different loops

조회 수: 3 (최근 30일)
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

채택된 답변

Doug Eastman
Doug Eastman 2011년 7월 8일
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
Paulo Silva
Paulo Silva 2011년 7월 9일
ok with large data it starts to make some sense
feng
feng 2011년 7월 9일
Thanks for starting understanding Haha

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2011년 7월 8일
Don't do this!!!
  댓글 수: 2
Jan
Jan 2011년 7월 8일
EVAL and CLEAR ALL in one example... It is worth to avoid both.
feng
feng 2011년 7월 9일
Good suggestions. Thank you very much!!!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by