Given handle to graphics object, determine which properties are read-only
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
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
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
per isakson
on 8 Jun 2014
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.
Cedric
on 8 Jun 2014
Edited: per isakson
on 8 Jun 2014
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?
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.
Matt J
on 8 Jun 2014
OK! Thanks.
More Answers (0)
Categories
Find more on Dates and Time in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)