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

4 views (last 30 days)
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?

Accepted Answer

per isakson
per isakson on 8 Jun 2014
Edited: per isakson on 8 Jun 2014
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 Comments
per isakson
per isakson on 8 Jun 2014
Edited: per isakson on 8 Jun 2014
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!