How can you create local variables from class properties?
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Is there a way to automatically create local variables corresponding to a class' properties?
Lets say I have a class instance foo, with properties A, B, C. I want to create local variables A, B, and C that are copies of foo.A, foo.B, and foo.C.
foo = fooclass;
A=foo.A;
B=foo.B;
C=foo.C;
Obviously, one could simply do the above -- however, for a case with a moderate number of parameters (and different parameters per situation), it would be nice if they could all be created in one go -- kinda like a smarter deal. For bonus points, the inverse operation would also be great. i.e.:
foo.A = A;
foo.B = B;
foo.C = C;
I can imagine writing a script that will do this in a given workspace -- but I don't see a way to make it a function that will create these variables in a calling workspace. Of course this uses eval (shudder) -- and you aren't supposed to do this -- and yes, I know my whole question is an elaborate version of creating variables with eval, which everyone should know better than. However, in this case, I think/hope it could actually result in less error prone code.
pnames = parameters(foo);
for i=1:length(pnames)
eval( [ pnames{i} ' = foo.' pnames{i} ';']);
end
I guess you could write a function that would build up a long eval string and return it, you could then just eval that...
eval( foo.abomination() );
6 Kommentare
Stephen23
am 2 Okt. 2019
Bearbeitet: Stephen23
am 2 Okt. 2019
"...forcing use of the class members all the time will likely be the solution (foo.A, foo.B, etc)"
That is exactly what I would recommend, and it seems to be the one simple solution that you are trying to avoid... Once I started doing this my own code also became easier to work with: easier to search, to follow the data flow, and identify origins of data. I see explicitly naming the class/structure/whatever as a benefit (not as a disadvantage), much like obtaining and using explicit graphics handles adds 10% work but gives a 20% benefit.
Antworten (1)
Matt J
am 1 Okt. 2019
Bearbeitet: Matt J
am 1 Okt. 2019
A better alternative to eval is to use my structvars FEX submission
It works on objects too. Example:
classdef myclass
properties
A
B
C
end
end
>> structvars(myclass)
A = S.A;
B = S.B;
C = S.C;
2 Kommentare
Matt J
am 4 Okt. 2019
I guess you could write a function that would build up a long eval string and return it, you could then just eval that... eval( foo.abomination() );
Note that the foo.abomination idea that you mention in your original post is exactly what structvars does.
eval(structvars(foo).')
Of course, I strongly advise against using it that way...
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!