Check if handle to axes exists at all [R2017b]

Hi,
I am trying to check if an axes handle exists and if not create it, e.g.:
X = [1:100];
Y=sin(X);
if handle exists
a=line(h, X, Y);
else
f=figure();
h=axes(figure);
end
In the case where it hasn't been created I keep getting an error 'Undefined function or variable' when I use ishandle or any get statement.

 採用された回答

Jan
Jan 2018 年 1 月 17 日
編集済み: Jan 2018 年 1 月 17 日

1 投票

The code is not meaningful. You can always know, if the axes has been created before or not. Therefore it is not clear, what you are exactly asking for. Maybe this helps:
f = gobjects(0);
h = gobjects(0);
% Then a magic section, which does not let you know if the axes is
% existing or not:
if rand > 0.5
f = figure;
h = axes; %
end
...
if ~isgraphics(h) % Better than ishandle
f = figure;
h = axes; %
end
line(x, 1:10, rand(1, 10))
So define the variable holding the handle as an empty graphics object at first. The empty matrix would work also. Then you can check if the variable contains a valid graphics object.

5 件のコメント

TJ
TJ 2018 年 1 月 17 日
This is a GUI where graphs will be plotted and overlaid on each other on a single set of axes. So if it is the first file that s selected then the handle to the axes doesn't exist yet, so I want to create it. If it exists then plot the data on the axes that already exists. I have three separate axes so I am only looking for a particular set called 'h'. So I do not want to overwrite anything in 'h' if it has already been used.
TJ
TJ 2018 年 1 月 17 日
Another scenario may be if you are plotting data from a GUI and then after a few plots you close your figure window. Then you would have to first check if the window is available each time before you plot. I am trying to make this as user friendly as possible.
Stephen23
Stephen23 2018 年 1 月 17 日
編集済み: Stephen23 2018 年 1 月 17 日
@TJ: you should not rely on slowly trawling through the workspace to see if something exists or not. Preallocate all of the handles that you will need at the start of your code (in one array if they are closely related) using gobjects. Then you can simply and efficiently test the status of those axes themselves using indexing and ishghandle or isgraphics (recommended).
See:
Jan
Jan 2018 年 1 月 17 日
In both scenarios using the method I've suggested works well. Example:
FigH = figure;
axes;
LineH = gobjects(0);
for k = 1:1000
if isgraphics(h) % of ishghandle(h)
if isgraphics(LineH)
LineH = plot(1:10, rand(1, 10));
else
set(LineH, 'YData', rand(1, 10));
pause(1);
end
else
disp('User has closed the figure');
break;
end
end
Here isgraphics is used to check, if the figure is still existing and if the line object has been created before, to allow a cheaper setting of YData instead of a re-creation.
Does this help?
TJ
TJ 2018 年 1 月 18 日
Hi Jan,
Using this I managed to achieve my goal. Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

TJ
2018 年 1 月 17 日

コメント済み:

TJ
2018 年 1 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by