2014-10-19 84 views
0

我想显示的情节,然后等待要么光标被点击的情节,用户通过按一个键来输入数字到Matlab。到目前为止,我知道如何单独使用这两种方法,但我不知道如何让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 

然后程序将始终等待用户在返回查找光标输入之前从键盘输入数字。但是我希望程序能够独立地响应两者。

解决方案是什么?

回答

0

我不认为你可以通过输入和输入得到想要的结果。
的alternartive方法是回调函数添加到您的身影:

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将被调用,在图中鼠标的位置将被显示。 在这里你可以添加你的代码到处理光标输入