필터 지우기
필터 지우기

How determine type of plot object?

조회 수: 12 (최근 30일)
Stephan Heise
Stephan Heise 2012년 2월 8일
편집: Julian Hapke 2016년 3월 14일
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.

채택된 답변

Julian Hapke
Julian Hapke 2014년 7월 11일
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
Julian Hapke
Julian Hapke 2016년 1월 18일
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 2016년 3월 14일
편집: Julian Hapke 2016년 3월 14일
just found out that pre 2014b
class(handle(h))
also does the trick

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by