do not work ishandle and ishghandle in a loop?

6 次查看(过去 30 天)
Hello all,
I have a program which read values from sensors. The program must stop reading when the figure is closed. I had a program where I used a callback linked to the serial to execute when a terminator was received, then I evaluated the contition of ishghandle and worked properly.
Now, I would like to have the reading code inside of a loop, which execution condition is ishghandle. Well, it seems that this function or ishandle do not work inside of a loop, because value once figure is executed is 1 and it does not change to 0 when the figure is closed. I tested placing the condition in the while or inside the while as below.
My version is 2018b
function read(a)
figure;
drawnow;
delete(instrfindall);
s = serial(a);
fopen(s);
p=ishandle(gcf);
while(1)
if p == 1
n = s.BytesAvailable;
if n == 6
m = fread(s,6);
disp(m);
disp(p);
end
p=ishandle(gcf);
else
break;
end
end
fclose(s);
end

回答(1 个)

Walter Roberson
Walter Roberson 2020-3-21
If there is no current figure, then gcf will create a new figure... which would have a valid handle.
function read(a)
fig = figure;
drawnow;
delete(instrfindall);
s = serial(a);
fopen(s);
p=ishandle(fig);
while(1)
if p == 1
n = s.BytesAvailable;
if n == 6
m = fread(s,6);
disp(m);
disp(p);
end
p=ishandle(fig);
else
break;
end
end
fclose(s);
end
  4 个评论
Stephen23
Stephen23 2020-3-21
编辑:Stephen23 2020-3-21
"Be very, very careful about using gcf, gca, etc. in your code."
Specifically: if you want to write efficient, robust code, do NOT use either gcf or gca.
Always obtain and refer to explicit graphics handles for all graphics objects that you need to refer to.
Walter Roberson
Walter Roberson 2020-3-21
gcbo is hypothetically an exception, as it does not change according to which object is "active". On the other hand, I have rarely used gcbo.
clf() and cla() do have uses, but only when you pass a graphics object to them instead of relying on the current object.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by