Insert and extract variables to and from main structure
Show older comments
My question is quite simple but I can not find a tip for doing the following.
I have a certain number of variables, suppose, a b They can be double, text, or also structure (for example a.str1=1 a.str2=2)
I need a way to store them into a main structure
C.a=a C.b=b
and them extract them in a second moment
a=C.a b=C.b
where a and b will be the original double, text or structure.
Currently I am storing them into database (.mat) but it slow down a lot being a lot of reading and writinfg processes.
Thank a lot.
E.
Answers (14)
Jan
on 1 Apr 2011
What is wrong with using:
C.a = a;
C.b = b;
% and them extract them in a second moment:
a = C.a;
b = C.b;
This seems to be a working solution already. I assume there are further demands.
Storing a variable in a MAT file is actually not a database.
Jan
on 1 Apr 2011
3 votes
This is not a reliable programming technique. Please read FAQ: Why is it advised to avoid using EVAL.
The dynamic creation of 100 variables will definitely cause a lot of troubles and reduce the efficiency of your program. Using EVAL instead of dynamic fieldnames like "C.(tmp(i).name)" has remarkable drawbacks when you debug the code. It would be much better to use "C.a" instead of creating "a" dynamically. Imagine your struct has the field "max". After assignin "max" as a variable concealed by EVAL, you might be surprised that "max(1:10)" will reply unexpected values. Finding and solving this bug will be a tediuous task. In opposite to that using "C.max" is fast and reliable.
Friedrich
on 1 Apr 2011
Hi Enrico,
a small (not perfect) example how you can do this is the following:
a = 3;
b = 4;
d.c = 'text';
d.g = 1;
tmp = whos;
for i=1:size(tmp,1)
eval(['C.',tmp(i).name,'=',tmp(i).name]);
end
I think you have to modify this to your own specific needs.
Friedrich
Friedrich
on 1 Apr 2011
Yes it is. You have to add a ; in the eval command. It should look like this:
eval(['C.',tmp(i).name,'=',tmp(i).name,';']);
Friedrich
Friedrich
on 1 Apr 2011
Hopefully I understand this correctly. Consider a given struct C like created in my example above. You can do the oposite thing by the following code:
tmp = fieldnames(C);
for i=1:size(tmp,1)
eval([tmp{i},'=C.',tmp{i},';'])
end
Friedrich
Friedrich
on 1 Apr 2011
Okay, this is going to be tricky now. My code reminds me on the movie inception but here we go:
Store.E.a=1;
Store.E.b=2;
Store.g = 3;
Store.t = 'text';
str = structfun(@isstruct,Store);
tmp = fieldnames(Store);
struct_in_struct = tmp(str);
struct_field_of_struct_in_struct = fieldnames(eval(['Store.',struct_in_struct{1}]));
eval(['Store.',struct_in_struct{1},'.',struct_field_of_struct_in_struct{1}])
eval(['Store.',struct_in_struct{1},'.',struct_field_of_struct_in_struct{2}])
I think you have to put some loops around it for your needs. But I think you can handle this by your own.
Friedrich
Friedrich
on 1 Apr 2011
1 vote
Thanks Enrico,
if you think this answer is correct, please mark it as accepted.
Greeting from the MathWorks technical Support from Germany,
Friedrich
Friedrich
on 1 Apr 2011
1 vote
Yeah sure it is not a reliable programming technique. But if you dont know the fieldnames during programming process you can't do anything else. And that was the issue the user was facing here.
1 Comment
Jan
on 1 Apr 2011
I'm relatively sure, that the names of the needed variables are known during programming. Except for the case, that the program run different branches triggered by "if exist('a', 'var')" - very prone to errors and horrible to debug.
Matt J
on 24 Aug 2017
1 vote
Here is a solution that avoids the dangers of EVAL,
Enrico
on 1 Apr 2011
0 votes
Enrico
on 1 Apr 2011
0 votes
Enrico
on 1 Apr 2011
0 votes
Enrico
on 1 Apr 2011
0 votes
Enrico
on 1 Apr 2011
0 votes
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!