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

graphicshandlepropertiesread-onlysmall furry creatures

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?

Best Answer

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.