How to pass the object name as a selectable property?

조회 수: 2 (최근 30일)
Sylvain
Sylvain 2020년 1월 21일
댓글: Guillaume 2020년 1월 22일
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