two consequtive cursor's position in a while loop

2 Ansichten (letzte 30 Tage)
Hello,
I have the following proiblem:
I am getting the cursor's poition inside a plot. The code returns only the current position of the cursor. Although I can see the previouse positios but i cant have access to them. What I want is to return the two consequtive position of the cursor (the current poistion and the previous one) inside the while loop.
For example imagine the current position of the cursor is (x1,y1). The next iteration the cursor's position would be (x2,y2). In the current iteration it is (x3,y3). I would like to get (x2,y2) and (x3,y3).
here is what I got so far:
function test_simpleMouseEvents01
SS = get(0,'ScreenSize');
figure('position',[10,10,SS(3)-100,SS(4)-200]); axis off; hold on;
set(gcf,'WindowButtonMotionFcn',{@wbm});
axis([-1,1,-1,1]);
x = [0, 0];
x1 = [];
disp('Move mouse to draw. Press right mouse button to quit.');
while 1
mb = get(gcf,'SelectionType');
if strcmp(mb,'alt')==1
close all;
return;
end
if double(get(gcf,'CurrentCharacter'))==27
close all;
return;
end
cur_point = get(gca,'Currentpoint');
x(1:2) = cur_point(1,1:2)'
assignin('base','x',[x(1,1);x(1,2)]);
plot(x(1), x(2), 'k.','markersize',20);
% x1 = [x1, x']
drawnow;
end
end
%% Mouse move
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function wbm(h,evd)
end

Akzeptierte Antwort

Bjorn Gustavsson
Bjorn Gustavsson am 4 Aug. 2022
Just after you have extracted and exported x in your while-loop you now have the next x-previous, so just stack that one away. Something like this:
function test_simpleMouseEvents01
SS = get(0,'ScreenSize');
figure('position',[10,10,SS(3)-100,SS(4)-200]); axis off; hold on;
set(gcf,'WindowButtonMotionFcn',{@wbm});
axis([-1,1,-1,1]);
x = [0, 0];
x1 = [];
x_prev = [];
disp('Move mouse to draw. Press right mouse button to quit.');
while 1
mb = get(gcf,'SelectionType');
if strcmp(mb,'alt')==1
close all;
return;
end
if double(get(gcf,'CurrentCharacter'))==27
close all;
return;
end
cur_point = get(gca,'Currentpoint');
x(1:2) = cur_point(1,1:2)'
assignin('base','x',[x(1,1);x(1,2)]);
assignin('base','x_prev',x_prev);
x_prev = x;
plot(x(1), x(2), 'k.','markersize',20);
% x1 = [x1, x']
drawnow;
end
end
%% Mouse move
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function wbm(h,evd)
end
First time through the loop x_prev will be empty but after that you're good to go. You might possibly want to initialize x_prev to something different that an empty array, like [0 0] or [-inf,-inf] if you need something of that size.
HTH
  2 Kommentare
MEHRDAD TAVASSOLI
MEHRDAD TAVASSOLI am 4 Aug. 2022
Thanks alot that was very helpful.
However I have a concern.
I need the mentioned two values be published real-time in a sence if move the mouse, I get the new values of x and x_prev.
In this formulation, I receive the values only if I kick tyhe right click or Esc bottom.
Is there any way to implement what I mentioned?
Thanks
Bjorn Gustavsson
Bjorn Gustavsson am 4 Aug. 2022
You should be able to modify (or at least utilize) GUI_27 from 41-complete-gui-examples.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by