Filter löschen
Filter löschen

do not work ishandle and ishghandle in a loop?

6 Ansichten (letzte 30 Tage)
Carlos Molina
Carlos Molina am 21 Mär. 2020
Bearbeitet: Stephen23 am 21 Mär. 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

Antworten (1)

Walter Roberson
Walter Roberson am 21 Mär. 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 Kommentare
Stephen23
Stephen23 am 21 Mär. 2020
Bearbeitet: Stephen23 am 21 Mär. 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 am 21 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by