Insert and extract variables to and from main structure

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.

Réponses (14)

Jan
Jan le 1 Avr 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
Jan le 1 Avr 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.
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
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
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
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
Friedrich le 1 Avr 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
Friedrich le 1 Avr 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 commentaire

Jan
Jan le 1 Avr 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.

Connectez-vous pour commenter.

Enrico
Enrico le 1 Avr 2011

0 votes

Of course what I reported was an example. I have nearly 100 variables, not only a and b.
So I search for an automatic procedure for doing this
Enrico
Enrico le 1 Avr 2011

0 votes

Thank Friedrich, your suggestion works really well. But whos function has got the problem of writing in the prompt variables name and so on, slowing up speed in case of lot of access to this function. I am doing this for avoing to write .mat file to disk. Is there a way to avoid writing in the prompt then? Thank again for kindness
Enrico
Enrico
Enrico le 1 Avr 2011

0 votes

Friedrich thank again, you have been so fast and clear.
May you suggest me also the way to do the opposite thing now, I mean pass from the structure to variable inside the structure.
Sorry me for being so boring! :)
Enrico
Enrico le 1 Avr 2011

0 votes

I already thought to use fieldname but it doesn't work correctly.
suppose I have
Store.E.a=1 Store.E.b=2
E.a=Store.E.a E.b=Store.E.b
With 'fieldname' I can only retrive E and not the complete structure.
Hope I have been clear enough. Thank again
Enrico
Enrico le 1 Avr 2011

0 votes

Thank again Friedrich, you should win the world cup as best matlab programmer.
All my thankfulness
Best regards,
Enrico

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Question posée :

le 1 Avr 2011

Réponse apportée :

le 24 Août 2017

Community Treasure Hunt

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

Start Hunting!

Translated by