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
0 个评论
回答(1 个)
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 个评论
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!