2014-10-19 85 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將被調用,在圖中鼠標的位置將被顯示。 在這裏你可以添加你的代碼到處理光標輸入