Evaluate string as structure

I wish to load a structure based on a string input and copy the contents to a new structure. I can do this with eval, but would prefer not to.
For example:
user_string = 'structure_name';
load( user_string )
new_data = eval( user_string );
The structure 'structure_name' has several fields and is saved as a .mat file.
Using (user_string) to evaluate the structure isn't an option

 Accepted Answer

Stephen23
Stephen23 on 1 Oct 2019
Edited: Stephen23 on 1 Oct 2019
The most important thing is to always load into an output variable, then your task is easy:
N = 'structure_name';
S = load(N); % load into an output variable (a scalar structure)
new_data = S.(N);
Or, if there is exactly one variable in the .mat file:
C = struct2cell(S);
new_data = C{1};

4 Comments

Thanks, this works but leaves me with a superfluous structure level - I can remove this manually but is it possible to do so as part of the load command ?
"Thanks, this works but leaves me with a superfluous structure level"
Actually the output new_data is exactly the variable that is stored in the .mat file. If that variable is numeric, then new_data will be numeric, if it is a structure (of any size), then new_data will be that structure, etc. The code I gave you does not include any "superfluous structure level" in the variable new_data. Lets try it and see:
>> T = struct('A',[1,2,3],'B','cat')
T =
A: [1 2 3]
B: 'cat'
>> save('test.mat','T');
>> clear
>> S = load('test.mat');
>> new_data = S.('T')
new_data =
A: [1 2 3]
B: 'cat'
"I can remove this manually..."
I don't see why you would bother, when you can just use the code that I gave you (which already removes the "superfluous structure level").
"...but is it possible to do so as part of the load command ?"
Not really. You can either magically load your data directly into the workspace (i.e. what you showed in your question) in which case you will then have to use eval to magically access that structure, or you can use the code that I gave you (which loads into a scalar structure and then removes the "superfluous structure level" and does not rely on awful eval).
The problem isn't with your solution, but more the data I have to access.
If the loaded data is of the form
structure_name.field_name_1
structure_name.field_name_2
When I load it in to the new variable I have
S.structure_name.field_name_1
S.structure_name.field_name_2
Whereas what I want is
S.field_name_1
S.field_name_2
As I said, easily done but would be neater if it could be donw at the load stage.
Stephen23
Stephen23 on 1 Oct 2019
Edited: Stephen23 on 1 Oct 2019
"As I said, easily done but would be neater if it could be donw at the load stage."
Sure, it might be neat.
But it isn't possible (for the reason I explained in my previous comment).
"When I load it in to the new variable I have ..."
Which is why I showed you two methods for removing the "superfluous structure level" without awful eval. However you seem to be only looking at the first part of my code (i.e. load into a scalar structure) and not at those two methods.
"The problem isn't with your solution, but more the data I have to access."
I don't see why your data is a problem at all. The similar examples I tried worked for me.
"Whereas what I want is"
Sure, and that is exactly what my code gives you.

Sign in to comment.

More Answers (1)

Categories

Products

Tags

Asked:

on 1 Oct 2019

Answered:

on 1 Oct 2019

Community Treasure Hunt

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

Start Hunting!