Borrar filtros
Borrar filtros

How determine type of plot object?

12 visualizaciones (últimos 30 días)
Stephan Heise
Stephan Heise el 8 de Feb. de 2012
Editada: Julian Hapke el 14 de Mzo. de 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.

Respuesta aceptada

Julian Hapke
Julian Hapke el 11 de Jul. de 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 comentarios
Julian Hapke
Julian Hapke el 18 de En. de 2016
Addon:
From 2014b on you can use the
class(h)
function to get information about your plot handle and its class.
Julian Hapke
Julian Hapke el 14 de Mzo. de 2016
Editada: Julian Hapke el 14 de Mzo. de 2016
just found out that pre 2014b
class(handle(h))
also does the trick

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by