Clear Filters
Clear Filters

do not work ishandle and ishghandle in a loop?

6 views (last 30 days)
Carlos Molina
Carlos Molina on 21 Mar 2020
Edited: Stephen23 on 21 Mar 2020
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

Answers (1)

Walter Roberson
Walter Roberson on 21 Mar 2020
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 Comments
Stephen23
Stephen23 on 21 Mar 2020
Edited: Stephen23 on 21 Mar 2020
"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 on 21 Mar 2020
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.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!