From: Samy Lyons on
Hi All-
I have this problem - there are some very large variables - say a b c that I'm trying to save to a mat file on every iteration of a for loop, then clear from memory because otherwise I run out of memory (a b and c are HUGE).

So I know how to change the name of the file with each iteration and index it so that I can open it appropriately once the memory has room for it again, however I realized that if the variable names are the same with each new mat file, I can't open them in my workspace.

For example:

filename=(['string_',num2str(index), '_' , num2str(index2]);
save(filename, 'a', 'b', 'c')

Then I get something like:
string_1_1.mat, string_1_2.mat ... etc.
But opening them I get a b c and then the next one I get a b c which replaces the original a b c

I would like to save them as a_1_1, b_1_1, c_1_1 but can't figure out how to do that.

How do you create a variable name in a string, then assign that variable to equal the original variable? This doesn't work:
aname=(['a_',num2str(index),'_',num2str(index2)]);
aname=a
nor does:
(['a_',num2str(index),'_',num2str(index2)])=a

Any ideas??

Thanks!
From: Ross W on
"Samy Lyons" <samanthe.lyons(a)gmail.com> wrote in message <i44bsd$c3$1(a)fred.mathworks.com>...
> Hi All-
> I have this problem - there are some very large variables - say a b c that I'm trying to save to a mat file on every iteration of a for loop, then clear from memory because otherwise I run out of memory (a b and c are HUGE).
>
> So I know how to change the name of the file with each iteration and index it so that I can open it appropriately once the memory has room for it again, however I realized that if the variable names are the same with each new mat file, I can't open them in my workspace.
>
> For example:
>
> filename=(['string_',num2str(index), '_' , num2str(index2]);
> save(filename, 'a', 'b', 'c')
>
> Then I get something like:
> string_1_1.mat, string_1_2.mat ... etc.
> But opening them I get a b c and then the next one I get a b c which replaces the original a b c
>
> I would like to save them as a_1_1, b_1_1, c_1_1 but can't figure out how to do that.
>
> How do you create a variable name in a string, then assign that variable to equal the original variable? This doesn't work:
> aname=(['a_',num2str(index),'_',num2str(index2)]);
> aname=a
> nor does:
> (['a_',num2str(index),'_',num2str(index2)])=a
>
> Any ideas??
>
> Thanks!

Hi

Is there room for two copies of a,b,c in memory at the same time?
Can you do this:
atemp=a;btemp=b;ctemp=c;
load(filename) %which brings in a,b,c

If your indexing is the best solution, you can use EVAL, but it's usually the wrong solution. See this link for why you shouldn't use use eval:
http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Ross
From: Jan Simon on
Dear Samy,

> filename=(['string_',num2str(index), '_' , num2str(index2]);
> save(filename, 'a', 'b', 'c')
>
> Then I get something like:
> string_1_1.mat, string_1_2.mat ... etc.
> But opening them I get a b c and then the next one I get a b c which replaces the original a b c
>
> I would like to save them as a_1_1, b_1_1, c_1_1 but can't figure out how to do that.

I'd strongly recommend not mix mix the names of the variables with an index describing the data structure.
It would be much clearer and easier to maintain, if you load the MTA files into a STRUCT instead:
data = cell{10, 10}
data{1, 1} = load('string_1_1.mat');
data{3, 7} = load('string_3_7.mat');
Then data{x,y} contains the fields a,b,c.

Good luck, Jan