2016-08-04 111 views
0

我想從顯示的圖像中獲取矩形座標。我想要移動圖像。這是我的matlab GUI。
enter image description here如何不停止正確的Matlab GUI?

所以,當我按下它應該顯示下一個圖像的系列和類似的後退按鈕。我使用此代碼

function next_Callback(hObject, eventdata, handles) 
% hObject handle to next (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 
if handles.mypointer~=length(handles.cur_images) 
    handles.mypointer=handles.mypointer+1; 
    pic=imread(fullfile('images',handles.cur_images(handles.mypointer).name)); 
    handles.imageName=handles.cur_images(handles.mypointer).name; 
    imshow(pic); 
    h=imrect; 
    getMyPos(getPosition(h)); 
    addNewPositionCallback(h,@(p) getMyPos(p)); 
    fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim')); 
    setPositionConstraintFcn(h,fcn); 
    handles.output = hObject; 
    handles.posi=getPosition(h); 
    guidata(hObject, handles); 

但這種代碼的缺點是,當按下一個按鈕則停在h=imrect所以等待用戶繪製矩形。它不做任何事,如果我不繪製一個矩形。即使我再次按下或下一個按鈕,它什麼都不做,因爲它仍在等待用戶繪製矩形。我很抱歉,如果這是一個明顯的問題,但我是matlab GUI新手。
問:
如何不要讓程序停止在Imrect

+0

爲「不正確」做一個單獨的按鈕... – excaza

回答

0

幾個注意事項:

  • 我認爲最好的做法是將imrect到單獨的按鈕,像Excaza提及。
  • 我無法重複您的問題 - 我創建了GUI示例,並且所有按鈕在執行imrect後都會響應。
  • 我也推薦執行h = imrect(handles.axes1);而不是'h = imrect;`(我不知道它是否與你的問題有關)。

當我遇到了Matlab「阻塞函數」的問題時,我通過在計時器對象的回調函數中執行函數來解決它。
我不知道這是否是一種好習慣,它只是我解決它的方法......
在定時器回調中執行一個函數,允許繼續執行主程序流程。

以下代碼示例顯示如何創建計時器對象,並在計時器的回調函數內執行imrect
該示例創建「SingleShot」計時器,並在執行時間start函數後觸發要執行的回調200毫秒。

function timerFcn_Callback(mTimer, ~) 
handles = mTimer.UserData; 
h = imrect(handles.axes1); %Call imrect(handles.axes1) instead just imrect. 
% getMyPos(getPosition(h)); 
% ... 

%Stop and delete the timer (this is not a goot practice to delete timer here - this is just an exampe). 
stop(mTimer); 
delete(mTimer); 


function next_Callback(hObject, eventdata, handles) 
% if handles.mypointer~=length(handles.cur_images) 
% ... 
% h = imrect(handles.axes1); 

%Create new "Single Shot" timer (note: it is not a good practice to create new timer each time next_Callback is executed). 
t = timer; 
t.TimerFcn = @timerFcn_Callback; %Set timer callback function 
t.ExecutionMode = 'singleShot'; %Set mode to "singleShot" - execute TimerFcn only once. 
t.StartDelay = 0.2;    %Wait 200msec from start(t) to executing timerFcn_Callback. 
t.UserData = handles;   %Set UserData to handles - passing handles structure to the timer. 

%Start timer (function timerFcn_Callback will be executed 200msec after calling start(t)). 
start(t); 

disp('Continue execution...');