【问题标题】:Matlab: allowing cursor and keyboard input simultaneouslyMatlab:允许光标和键盘同时输入
【发布时间】:2014-12-14 21:28:37
【问题描述】:

我想显示一个绘图,然后等待或者光标在绘图上被点击,或者用户通过按a在Matlab中输入一个数字钥匙。到目前为止,我知道如何分别处理这两个问题,但我不知道如何让 Matlab 独立响应这两个问题。我想你会称之为多线程的一种形式。

单击光标时启用响应的代码如下:

h = figure( %{ ... params ...%} );
while true
    figure(h);
    cursor = ginput(1);
    % ... process the cursor input ...
end

现在,我想包含以下行以使用户能够从键盘输入数字:

num = input('Enter the number: ' );

但如果我只是将它添加到我的 while 循环中:

h = figure( %{ ... params ...%} );
while true
    figure(h);
    cursor = ginput(1);
    % ... process the cursor input ...
    num = input('Enter the number: ' ); 
    % ... process the keyboard input ...
end

然后程序将始终等待用户从键盘输入数字,然后返回寻找光标输入。但我希望程序能够相互独立地响应。

解决办法是什么?

【问题讨论】:

    标签: matlab matlab-figure


    【解决方案1】:

    我不认为你可以通过 ginput 和 input 获得想要的结果。
    另一种方法是为您的图形添加回调函数:

    function mouse_and_keyboard_input()
        %set up a figure with keypress and mouseclick event
        figure('KeyPressFcn',@keyPress, 'WindowButtonDownFcn', @mouseClicked)
        plot(sin(0:0.1:2*pi));
    
        h_text = text(10, -0.8, 'No key pressed');
        h_mouse = text(10, -0.6, 'No mouse click');
    
            % Nested function: variables are shared.
            function keyPress(objectHandle, eventData)
                key = get(objectHandle,'CurrentCharacter');
                set(h_text, 'string', ['Last pressed key: ' key])
            end 
            function mouseClicked(objectHandle , eventData )
                disp('mouse clicked')
                pos = get(objectHandle,'CurrentPoint'); 
                set(h_mouse, 'string', ['Mouse click pos: ' num2str(pos)])
            end 
    end 
    

    如果单击图形,将调用函数mouseClicked,并显示鼠标在图形中的位置。 在这里您可以添加更多代码来处理光标输入

    【讨论】:

      猜你喜欢
      • 2013-12-06
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多