question about pause function,and real time drawing

Hello!
I use a drawing tablet and i manage to get and save x,y coordinates.
if i want to see what i draw, i use the command
line(x, y, 'Color','k','LineWidth', 1);
If i want to see in real time what i am drawing i use
line(x, y, 'Color','k','LineWidth', 1);
pause(0.001);hold on
The problem is when i use pause, i miss some packets from the tablet,so the drawing is kind of squared and it is different from the non real time drawing.
Any idea what to do at the real time drawing?
Thank you very much!

 Respuesta aceptada

Geoff Hayes
Geoff Hayes el 28 de Oct. de 2014
Alex - what you are working on sounds cool.
When you call
line(x, y, 'Color','k','LineWidth', 1);
for the real time drawing, do x and y contain all coordinates or only the most recent ones that were sent from the tablet? If the former, then you could try updating the line object instead of creating a new one with each call to line. Presumably you have created a figure (or are drawing on an axes within a GUI), so you could initialize your line as
hLine = line(NaN,NaN, 'Color','k','LineWidth', 1);
Now, when you receive new coordinates, you could just update the current line as
x = [x ; newX];
y = [y ; newY];
set(hLine,'XData',x,'YData',y);
Then, remove the pause and hold statements and replace both with just the drawnow as
x = [x ; newX];
y = [y ; newY];
set(hLine,'XData',x,'YData',y);
drawnow;
The above might speed things up a bit so that you are less likely to miss packets. Try it and see!

7 comentarios

alex
alex el 29 de Oct. de 2014
thank you very much Geoff! I will try your code and i will let you know! it seems very helpful!
alex
alex el 29 de Oct. de 2014
Editada: alex el 29 de Oct. de 2014
yes x and y contain all coordinates. not all exactly. they contain the coordinates since the last time the pen touched the tablet. if the pen doesnt touch the tablet i empty the x and y. i do that because, if for example i want to write the letter 'a' and then the letter 'b' , if i will not empty the x and y, then there is a line that connects the last point of the 'a' with the first point of the 'b'
Anyway, thanks again, i will let you know with the results!
it seems to work!
However i have a problem
the old code was
while 1
if pentouschestablet
x(i)=newX;
y(i)=newY;
i=i+1;
line(x, y, 'Color','k','LineWidth', 1);
end
if pennottouschestablet
i=1;
x=[];
y=[];
end
pause(0.001);
hold on
end
the new code is
hLine = line(NaN,NaN, 'Color','k','LineWidth', 1);
while 1
if pentouschestablet
x = [x ; newX];
y = [y ; newY];
set(hLine,'XData',x,'YData',y);
drawnow;
end
if pennottouschestablet
x=[];
y=[];
end
end
the new code works fine,with the difference that when i draw a letter for example and i raise the pen from the tablet to draw another letter, the first letter dissappears.I didnt have this problem with the old code.
Any idea?
The line of code
set(hLine,'XData',x,'YData',y);
replaces the x and y data for the line graphics object (referenced by hLine) with the new set of x and y data, and you call this line whenever the pentouschestablet is true. When it is false (or rather when pennottouschestablet is true), the code resets x and y to the empty matrices, and the next time the pen touches the tablet, we call
set(hLine,'XData',x,'YData',y);
where x and y now only have the first position of where the pen touches the tablet. All information that we had before (for the previous letter) is replaced with the new point (and future points) for the new letter.
What you will need to do, is to create another graphics object for the next letter as
if pennottouschestablet
x=[];
y=[];
hLine = line(NaN,NaN, 'Color','k','LineWidth', 1);
end
With the above, every time the pen leaves the tablet and then returns to it, we will use a new (line) graphics object to draw a shape, letter, etc..
(With the old code, it kept creating new line graphics objects so you never lost any data...you just ended up with perhaps more graphics objects than necessary.)
So try adding the third line to your second if body, and see what happens. I don't think that you will need to do a hold on for your axes (a quick test showed that I didn't for line objects) so it should work.
It may be worthwhile creating an array of these line handles, just so you have the ability to reference them in the future (say if you want to undo the last character/letter drawn, then just delete the handle for it and it will disappear from your axes). You could probably do something like
hLines = line(NaN,NaN, 'Color','k','LineWidth', 1);
while 1
if pentouschestablet
x = [x ; newX];
y = [y ; newY];
set(hLines(end),'XData',x,'YData',y);
drawnow;
end
if pennottouschestablet
x=[];
y=[];
hLines = [hLines line(NaN,NaN, 'Color','k','LineWidth', 1)];
end
end
Note how we just use hLines(end) to get the last handle in our array, and we just concatenate hLines with the new handles whenever the pen leaves the tablet.
Out of curiosity, what happens if there is a five second delay between when the pen leaves the tables and when it touches it again? Does the while loop continue to run? I only ask, because it it does, then we will be creating new handles when we don't need to. You may want to guard against this by assuming that if x and y are empty, then there is no need to create a new handle
if pennottouschestablet
if ~isempty(x)
x=[];
y=[];
hLines = [hLines line(NaN,NaN, 'Color','k','LineWidth', 1)];
end
end
In the above, we check to see if x is empty. If not, then set x and y to be empty, and create the new line graphics object.
alex
alex el 30 de Oct. de 2014
you are very analytical! thank you very much!
As far as the last part is conserned, i'm sure that the while loop will keep running, so we will create new handles. so the "if ~isempty(x)" statement is a nice add!
I will refresh the code and i will let you know!
alex
alex el 30 de Oct. de 2014
just perfect! everything works fine! thank you very much Geoff!
p.s: the while loop continues to run normally (without the if ~isempty(x) )for 10 seconds without the pen touching the tablet.there was no problem!
Thank you very much again!
Glad that it is working well, Alex!

Iniciar sesión para comentar.

Más respuestas (1)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 27 de Oct. de 2014

Comentada:

el 30 de Oct. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by