Given handle to graphics object, determine which properties are read-only

Sometimes I find I'd like to copy all properties of a graphics object to another object, excluding the ones that are read-only. I.e., I would like to avoid errors like the following where h1 and h2 are both text object handles,
>> set(h2,get(h1))
Error using set
Attempt to modify a property that is read-only.
Object Name: text
Property Name: 'Annotation'.
Is there an easy way to filter out the read-only properties?

 采纳的回答

Yes
set_properties = set( h2 );
get_properties = get( h2 );
read_only_properties = setdiff( fieldnames( get_properties ) ...
, fieldnames( set_properties ) );
a = set(h) returns the user-settable properties and possible values
for the object identified by h. [...]
and on get
a = get(h) returns a structure whose field names are the object's
property names and whose values are the current values of the
corresponding properties. [...]
.
In response to comment:
The hack cssm below does nearly implement set(h2,gp). (The functions isflint and dbs are attached.)
function cssm( )
figure;
subplot(121); plot(1:5); h1=gca;
subplot(122); plot(5:-1:1); h2=gca;
sp = set( h1 );
gp = get( h1 );
rop = setdiff( fieldnames( gp ), fieldnames( sp ) );
for ii=1:length(rop) %get rid of read-only properties of h1
gp = rmfield( gp, rop{ii} );
end
for name = transpose( fieldnames( gp ) )
try
if not(isempty( gp.(name{:}) )) ...
&& all(all(ishghandle( gp.(name{:}) )))
if not(any(isflint( gp.(name{:}) )))
copyobj( gp.(name{:}), h2 )
else
set( h2, name{:}, gp.(name{:}) )
end
else
if not(any( strcmpi( name, {'Position' ...
,'OuterPosition'} ) ))
set( h2, name{:}, gp.(name{:}) )
end
end
catch me
dbs
17;
end
end
end
.
Many properties have the value 0 or 1. ishghandle returns true for both of these (given that figure 1 exists). if not(any(isflint( gp.(name{:}) ))) spots these values.

6 个评论

That's quite interesting. But then why doesn't the following work, as way to copy axes h1 to axes h2
figure;
subplot(121); plot(1:5); h1=gca;
subplot(122); plot(5:-1:1); h2=gca;
sp = set( h1 );
gp = get( h1 );
rop = setdiff( fieldnames( gp ) ...
, fieldnames( sp ) );
for ii=1:length(rop) %get rid of read-only properties of h1
gp=rmfield(gp,rop{ii});
end
set(h2,gp)
For me this produces the error message,
Error using set
Children may only be set to a permutation of itself
Matlab says
>> why
To fool the tall good and smart system manager.
and we have to accept that answer together with the error message ;-)
I'll add a modified version of your code to my answer.
Interesting, this error cannot be reproduced all the time, because the cause varies..
>> why
The bald and not excessively bald and not excessively smart hamster obeyed a
terrified and not excessively terrified hamster.
Mine says that this is a large furry bug with big pointy teeth
:-|
I'll add a modified version of your code to my answer.
But what is the cause of the error my original version produces? What does " Children may only be set to a permutation of itself " mean?
Once the read-only properties of gp are removed, why doesn't
set(h2,gp)
work directly?
AKAIK: set( h, 'Children', children_handles ) can only be used to changed the "order" of the children, where "order" affects z-order, tab-order or whatever is applicable.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by