2016-08-04 168 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...');