How to pass the object name as a selectable property?

2 次查看(过去 30 天)
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

采纳的回答

Guillaume
Guillaume 2020-1-21
编辑:Guillaume 2020-1-22
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 个评论
Sylvain
Sylvain 2020-1-22
Merci Guillaume
Just tried this, it was not working, till I change the commad addProp to addprop.
Guillaume
Guillaume 2020-1-22
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by