필터 지우기
필터 지우기

do not work ishandle and ishghandle in a loop?

조회 수: 6 (최근 30일)
Carlos Molina
Carlos Molina 2020년 3월 21일
편집: Stephen23 2020년 3월 21일
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