필터 지우기
필터 지우기

User input during animation

조회 수: 1 (최근 30일)
Ted Theodosopoulos
Ted Theodosopoulos 2021년 6월 29일
댓글: Walter Roberson 2021년 7월 3일
I have created an animation that graphs the motion of a test particle under the influence of a force field in 2D and I want to add a user interface. I'd like the user to be able to nudge the test particle by using the keyboard, e.g. the arrow keys. I'd like the user input to be interpreted as a variable in the program, so it can be added to the internal variables that are being graphed. I'd like this to happen without interrupting the animation. How might this be possible to accomplish in Matlab?
Ted
  댓글 수: 2
Jan
Jan 2021년 6월 30일
It depends on how you have implemented the animation yet. Is it an animated GIF, a figure or uifigure with a loop? Is it a simulink application?
Ted Theodosopoulos
Ted Theodosopoulos 2021년 7월 1일
My animation is based on a simple for-loop with short pauses between the plotting of individual points of a trajectory. I'd like the user to be able to influence this trajectory, as they are watching it play out. Does this make sense?
Ted

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2021년 7월 1일
Use a WIndowsKeyPressFcn callback, and check the event information to figure out which key was pressed in order to figure out what changes to make to your variables. Use one of the methods of sharing variables to get the data to the compute loop.
Your compute loop will need to periodically pause() or drawnow() to receive the interrupts, but it has to use one of those to make the animation visible anyhow.
  댓글 수: 2
Ted Theodosopoulos
Ted Theodosopoulos 2021년 7월 3일
Thank you for this information. Unfortunately, I am not familiar with the callback functionality in Matlab. Could you possibly send me a short example code that shows how to use it? You may assume that we are in a loop, we've just plotted a point in a numbered figure (say figure(1)) and that we are in a timed pause (e.g. pause(0.05)) as part of the animation. What command can I use that will return the button, if any, that the user has just pressed? Do I need to write a subroutine that accomplishes that? If so, what should it look like? Thanks so much for your continued support with this inquiry.
Ted
Walter Roberson
Walter Roberson 2021년 7월 3일
function fig1_key_callback(hObject, event, handles)
xv = handles.particle_xv;
yv = handles.particle_yv;
switch event.Key
case 'leftarrow'
xv = xv - 5;
case 'rightarrow'
xv = xv + 5;
case 'uparrow'
yv = yv + 5;
case 'downarrow'
yv = yv - 5;
case 'q'
handles.quit = true;
otherwise
%do nothing
end
handles.particle_xv = xv;
handles.particle_yv = yv;
guidata(hObject, handles)
end
function processing_loop(hObject, event, handles)
%stuff
handles.x = x0; handles.y = y0; handles.quit = false;
handles.xv = randi([-10 10]); handles.yv = randi([-10 10]);
guidata(hObject, handles);
h = animatedline(handles.x, handles.y);
while true
handles = guidata(hObject); %pull back updates to handles
if handles.quit
handles.quit = false;
break;
end
handles.x = handles.x + handles.xv;
handles.y = handles.y + handles.yv;
addpoints(h, handles.x, handles.y);
guidata(hObject, handles);
pause(0.05);
end
end
In this demonstration, the user does not control the x or y position directly, and instead affects the velocity, nudging it higher or lower (including negative).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by