How to pass the object name as a selectable property?

2 vues (au cours des 30 derniers jours)
Sylvain
Sylvain le 21 Jan 2020
Commenté : Guillaume le 22 Jan 2020
Hi all,
I have started to use some hgtransform objects and started to group them. The idea is to build generic configurable graphical objects.
here is an axample of what I am doing:
Step 1: create the main assembly containing parts,:
ASSEMBLY = hgtransform();ASSEMBLY.Tag = 'ASSEMBLY';
PART1 = hgtransform('Parent',ASSEMBLY); PART1.Tag = 'PART1';
PART2 = hgtransform('PARENT',ASSEMBLY);PART1.Tag = 'PART2';
[cx,cy,cz]=cylinder([0.1 0.1]); % get coordinates from cylinder function
C1=surface(cz,cy,cx,'FaceColor','red','EdgeColor','none','FaceAlpha',.2,'Parent',PART1); % create a surface and assign the surface to the PART1
C1.Tag ='CYLINDER'
% PART1 contains a cylinder with a lovely red color
copyobj(PART1,PART2); %PART2 contains a cylinder with a lovely red color
%% Translate the PART2 to avoid graphic overlap
set(PART2,'Matrix', makehgtform('translate',[1 0 0]))
Step 2: change the color of the :
ASSEMBLY.Children(1).Children.FaceColor = 'blue';
As my Assembly object is containing more and more hgtransform objects, I would like to have a more usable way to compute Step2 by invoking the Tag. I would like to have my code to look like :
ASSEMBLY.PART1.Children.FaceColor = 'blue';
Or even better
ASSEMBLY.PART1.CYLINDER.FaceColor = 'blue';
What would be the most elegant way to do it without rewritting a class object ?
kind regards
Sylvain

Réponse acceptée

Guillaume
Guillaume le 21 Jan 2020
Modifié(e) : Guillaume le 22 Jan 2020
You can, since hgtransform support dynamic properties, so you could do something like:
%create the properties:
ASSEMBLY.addprop('PART1');
ASSEMBLY.addprop('PART2');
PART1.addprop('CYLINDER');
%assign a value to the properties:
ASSEMBLY.PART1 = PART1;
ASSEMBLY.PART2 = PART2;
PART1.CYLINDER = C1;
%now you can do:
ASSEMBLY.PART1.CYLINDER.FaceColor = 'blue';
It's a bit clunky however and note that dynamic properties are not copied with copyobj.
  2 commentaires
Sylvain
Sylvain le 22 Jan 2020
Merci Guillaume
Just tried this, it was not working, till I change the commad addProp to addprop.
Guillaume
Guillaume le 22 Jan 2020
edited. One annoying thing about matlab is that there's no consistent capitalisation of function names. Some functions have their second word capitalised, others don't.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by