Dynamically Naming Variables Saved to A Mat File

Hello,
I am trying to store multiple structures as separate variables in a mat file so that I can then reload these structures separately. I do not want to place them into a cell-array, because then I would have to load the whole cell array at once from the mat file.
The problem is this involves dynamic naming of variables from a nested function, since the variables must have different names.
With this in mind how can I: Store an unknown number of structures in a SINGLE mat file, such that I can load each INDIVIDUALLY without loading the others.

Answers (3)

When you store multiple structures in a SINGLE mat file, you still need to load all the structures in that mat file. When you say "load", do you mean load the mat file, or load the variable. If all the data are in a single mat file, you can't save memory by saving variables in multiple structure, rather than saving in a single structure. For example, you can do:
Str.a=1:10
Str.b=1:100
Str.c=1:1000
or
a=1:10
b=1:100
c=1:1000
You can still reference individual variable in the first scenario:
NewVariable=Str.a
But the first scenario give you much flexibility using the dynamic field name method.

2 Comments

I mean load into the current workspace. The load function can have the following syntax: S = load(filename, variables). Allowing me to load only certain Variables. If a,b,and c were all in Str as you suggested I can't load them separately, I can only load Str.
I see. Then you can run assignin() in your nested function to achieve your goal.
VarName='a';
assignin('base',VarName,1:10);
You got the variable a assigned in the base workspace.

Sign in to comment.

What exactly are "multiple structures"? Saving and loading several STRUCTs is easy:
S1.a = 5;
S1.b = 1;
S2.a = 6;
S2.c = 7;
save('FileName.mat', 'S1', 'S2');
...
Data = load('FileName.mat', 'S1');
This solves your original question "Store an unknown number of structures in a SINGLE mat file".
But do you want to load only "S2.a"? Then I do not see the reason why this variable is stored as field of a STRUCT. Please clarify the wanted inputs and outputs exactly again.

2 Comments

No, that is a known number of structures (in your example 2) in one mat file. I want a way that I can continue the pattern for any number of structures S3,S4,S5, etc... but that requires dynamically naming variables.
I don't want to load only S2.a, I want to load only S2.
I still do not get your problem. You can create the fields "T.S1" to "T.S3047" using dynamic field names and save them using "save('FileName.mat', '-struct', 'T')". Afterwards "load('FileName.mat', 'S1089')" loads this field only.

Sign in to comment.

Usually, people advocate using cell arrays as a way of avoiding the evil "eval()" function. But, if you really need separate variables, eval() may be the way to go.
You can see a discussion of the issue in section 4.6 here: http://matlab.wikia.com/wiki/FAQ

1 Comment

I tried using eval and its other forms (evalin), but because I'm using it to dynamically name variables from a nested function it returns an error.

Sign in to comment.

Asked:

on 19 Jun 2011

Community Treasure Hunt

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

Start Hunting!