Is there a way to provide non-modal user input?

9 次查看(过去 30 天)
I'm using a plot to produce a 2D animation of a machine's operation by moving the position of a bunch of rectangles. I use pause commands to control the timing and sequencing of the animation. Sometimes I would like to have the animation move much faster than real time, other times much slower than real time. Presently, I predefine a "TimeExpansionFactor" which is greater or less than one to handle time compression and expansion of the animation. I would like to alter this TimeExpansionFactor during the program execution by pressing a "+" or "-" key during the program execution to speed up or slow down the animation. I do have convenient points in the program to process such requests from the user, but I don't want to use the Matlab "input" command since the program execution would stop running and REQUIRE user input. Rather, I would prefer to service a user request queue at particular point in the program. If the queue in empty, then just move on since the user has not provided any commands. If there is any input, then either speed up or slow down the animation as requested. Is there a simple way to do this in Matlab?

采纳的回答

Brandon Eidson
Brandon Eidson 2017-1-5
Hey James, I understand you have a script that is creating a 2D animation by calling the "plot" function multiple times and that you have implemented a way to change the animation speed based on the value of a variable called "TimeExpansionFactor". I further understand that you want to be able to change the value of "TimeExpansionFactor" without pausing execution of the animation.
If the above if correct, the most straightforward want to accomplish your goal is using a figure's "Key-press callback function". You can read about this function more at the link below.
MATLAB automatically passes an object containing data that has the character that was pressed. You can write some conditional statements checking the character and, assuming your define the "KeyPressFcn" in the scope of the function that contains the variable, you can update "TimeExpansionFactor" accordingly.
You could also check, in the callback, for the last key pressed using the Figure's "CurrentCharacter" property. In the callback you might have something like
if double(get(yourFigureHandle,'CurrentCharacter')) == 30 % i.e., if UP arrow was pressed
%adjust TimeExpansionFactor to make animation faster
elseif double(get(yourFigureHandle,'CurrentCharacter')) == 31 % i.e., if DOWN arrow was pressed
%adjust TimeExpansionFactor to make animation slower
end
Also, make sure you do not make a new figure each time you plot but just update the "YData" of the current axis.
You can find more information about callbacks in general at the documentation linked to below.

更多回答(1 个)

Jan
Jan 2017-1-5
Instead of pause commands, the animation can be driven by a timer. This allows to consider the time needed for drawing the image. The already mentioned KeypressFcn of the figure is the best way to catch user interactions. you can either store the TimeExpansionFactor in the figures UserData oder ApplicationData, or even better in the UserData of the timer object, where the timer's callback can access it directly.
  1 个评论
James Eder
James Eder 2017-1-6
Thanks for the extra tip. I knew that there was some overhead in drawing the figures which I was ignoring for the moment. Right now, the TimeExpansionFactor is in the main function. I didn't realize that a figure had User Data. I'll work this suggestion into my code as well.

请先登录,再进行评论。

类别

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

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by