How determine type of plot object?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Stephan Heise
le 8 Fév 2012
Modifié(e) : Julian Hapke
le 14 Mar 2016
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 commentaires
Réponse acceptée
Julian Hapke
le 11 Juil 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 commentaires
Julian Hapke
le 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
le 14 Mar 2016
Modifié(e) : Julian Hapke
le 14 Mar 2016
just found out that pre 2014b
class(handle(h))
also does the trick
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Type Identification dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!