运行ginput同时在MATLAB中运行循环 [英] Running ginput while also running a loop in MATLAB

查看:108
本文介绍了运行ginput同时在MATLAB中运行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用MATLAB开发的一款游戏中显示十字准线.我尝试使用ginput,因为它非常完美,因为它显示十字准线并在单击的x和y位置读取.但是,由于它总是等待单击并且我有移动的对象,因此它会导致在屏幕上创建对象,然后再将其移动.如何在ginput也正在运行并评估点击次数的情况下运行循环并继续移动对象?

I want to display a crosshair in one of the games I'm making in MATLAB. I tried using the ginput because it would be perfect as it displays crosshairs and reads in the x and y locations of what was clicked. However, since it always waits for a click and I have moving objects, it causes the object to be created on the screen and then not move. How do I get the loop to run and continue moving the object while ginput is also running and evaluating clicks?

推荐答案

非常类似于总结:ginput不适用于实时交互功能,实际上更多地不是注释工具.查看该图的ButtonDownFcn属性.该链接还包括一个有关如何实现此类操作的小示例.为了清楚起见,我将在此处进行复制,但是原始版权归 pm89

To summarize: ginput is not good for real time interactive functions and really more of an annotation tool. Look at the ButtonDownFcn property of the figure. The link also includes a small example of how you would implement something like this. I will copy here for clarity but original credit goes to pm89 and grantnz

% Stop button
uicontrol(...
    'Style','pushbutton', 'String', 'Stop',...
    'Units','Normalized', 'Position', [0.4 0.1 0.2 0.1],...
    'Callback', 'run = 0;');

% Axes
ax = axes(...
    'Units','Normalized',...
    'OuterPosition', [0 0.2 1 0.8]);

run = 1;
t = 0;
while run
    t = t + 0.01; x = t:0.01:t+2;
    h = plot(ax, x, sin(x));
    set(ax, 'ButtonDownFcn', 'get(ax, ''CurrentPoint'')');
    xlim([x(1) x(end)]); ylim([-1 1]);
    pause(0.01);
end

这篇关于运行ginput同时在MATLAB中运行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆