How determine type of plot object?
11 views (last 30 days)
Show older comments
Say, I am given an axes containing unknown plot objects. I am searching for a way to determine the type of these plot objects, i.e. I would like to know whether they are a lineseries object, a barseries object, an errorbarseries object, etc.
For core graphics objects (e.g. line, patch, surface, text,...) this isn't a problem because one can query the object's 'type' property,
get(h, 'Type').
However, most plot objects such as barseries or areaseries return 'hggroup' as type.
I have tried to find unique properties of each plot object type (e.g. 'BarPeers' for barseries objects) and then used a cascade of isprop calls to identify the plot object, but this obviously isn't very elegant.
Matlab itself seems to know the plot object type, because e.g.
set(handle_of_barseries_object, 'Color','r')
returns the error message "The name 'Color' is not an accessible property for an instance of class 'barseries'." So the plot object type must be determinable somehow.
Any ideas?
Stephan.
0 Comments
Accepted Answer
Julian Hapke
on 11 Jul 2014
here's the probably dirtiest code I've written so far, but does, what you want:
% make an example plot with several classes
figure;
hold on;
X=rand(10,1);Y=rand(10,1);X=sort(X);Y=sort(Y);
plot(X,Y);
errorbar(X,Y);
bar(X,Y);
barh(X,Y);
contour([X Y],[Y X]);
plts = get(gca,'Children');
% determine plot class by catching the error message that is
% generated when a nonexistent property is addressed
type = cell(size(plts));
for kk = 1:length(plts)
try
get(plts(kk),'nonexistent')
catch err
tmp = strfind(err.message,'''');
type{kk} = err.message(tmp(end-1)+1:tmp(end)-1);
end
end
regards
julian
3 Comments
Julian Hapke
on 18 Jan 2016
Addon:
From 2014b on you can use the
class(h)
function to get information about your plot handle and its class.
Julian Hapke
on 14 Mar 2016
Edited: Julian Hapke
on 14 Mar 2016
just found out that pre 2014b
class(handle(h))
also does the trick
More Answers (0)
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!