how do I create a handle to a plot without plotting?

Hello, here is my code
function handle = DrawLink(z,w,h,handle)
pts = [z-w/2, z+w/2, z+w/2, z-w/2; 0, 0, h, h];
X = pts(1,:);
Y = pts(2,:);
if isempty(handle)
handle = fill(X,Y,'b');
end
end
I am trying to create a handle to the plot without plotting it while I make it. How do I do this? Thanks.

回答 (2 件)

Star Strider
Star Strider 2018 年 1 月 9 日

1 投票

Try this:
fh = figure(1);
set(fh, 'Visible','off')
plot(rand(1,10), rand(1,10), 'p')
Then later, when you want to see it:
set(fh, 'Visible','on')
and it magickally appears!

4 件のコメント

Rick Giovanini
Rick Giovanini 2018 年 1 月 9 日
I like this, I'm a bit confused on how I can turn my handle into the figure though. It's a bit more complicated then just not showing a figure, and then showing it. I specifically need a handle to the points, first.
Star Strider
Star Strider 2018 年 1 月 9 日
I am not certain what you are doing, or the reason you are passing the handle as an argument and returning it as an output.
I would return the ‘pts’ variable as a separate output, along with ‘handle’, created in the function rather than passed to it. You can set the figure handle to 'Visible','off' inside the function, and then set it to 'Visible','on' outside the function.
In your code, ‘handle’ is to a patch object. Making it invisible with:
set(handle, 'Visible','off')
simply makes the patch object invisible while leaving the figure object unaffected.
Something like this could work:
function [handle, fh, pts] = DrawLink(z,w,h,handle)
pts = [z-w/2, z+w/2, z+w/2, z-w/2; 0, 0, h, h];
X = pts(1,:);
Y = pts(2,:);
if isempty(handle)
fh = figure;
set(fh, 'Visible','off')
handle = fill(X,Y,'b');
end
end
That returns everything of interest so you can work with it outside your function later.
I am guessing what you are doing, so this is just a suggestion.
Rick Giovanini
Rick Giovanini 2018 年 1 月 10 日
So, I don't want to turn the whole plot off because I have other things that are plotted, and I have these things animated. I just want a handle to this, and then I need to do something to determine if I need to rotate it, and then to plot it, or just plot it first of all. In order to rotate without plotting, I need to have this handle.
Star Strider
Star Strider 2018 年 1 月 10 日
I do not understand what you want to do, or what your requirements are.

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

Pablo Saavedra G.
Pablo Saavedra G. 2019 年 2 月 18 日

0 投票

You can always create an empty handle at the current axes and pass it to your function, for example:
handle = plot([],[],'-');
then in your function, the line with the if will get a TRUE
if isempty(handle)
handle = fill(X,Y,'b'); % note that previous handle (from plot) is repalced to fill
end

カテゴリ

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

タグ

質問済み:

2018 年 1 月 9 日

回答済み:

2019 年 2 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by