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.

답변 (14개)

Jan
Jan 2011년 4월 1일
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 2011년 4월 1일

3 개 추천

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
Friedrich 2011년 4월 1일
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
Friedrich 2011년 4월 1일
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
Friedrich 2011년 4월 1일
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
Friedrich 2011년 4월 1일
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 2011년 4월 1일

1 개 추천

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 2011년 4월 1일

1 개 추천

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

Jan
Jan 2011년 4월 1일
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.

댓글을 달려면 로그인하십시오.

Enrico
Enrico 2011년 4월 1일

0 개 추천

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 2011년 4월 1일

0 개 추천

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 2011년 4월 1일

0 개 추천

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 2011년 4월 1일

0 개 추천

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 2011년 4월 1일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

질문:

2011년 4월 1일

답변:

2017년 8월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by