two consequtive cursor's position in a while loop

1 次查看(过去 30 天)
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

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2022-8-4
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 个评论
MEHRDAD TAVASSOLI
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by