MATLAB GUIDE按钮必须按两次?

时间:2014-09-09 15:52:41

标签: matlab matlab-guide

我正在制作带指南的GUI。我有一个用户点击的按钮,回调如下:(重要的是真正的两条线......

   function SetParticleRoiSize_Callback(hObject, eventdata, handles)
% hObject    handle to SetParticleRoiSize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
particleroiSize=imrect;% - draw a rectagle around the particle to get a meausr eof ROI size
roiPoints=getPosition(particleroiSize); %-get tha parameters fo the rectanlge
partX1 = round(roiPoints(1));
partY1 = round(roiPoints(2));
partX2 = round(partX1 + roiPoints(3));
partY2 = round(partY1 + roiPoints(4)); % these are the ROi positions in pixels

roiHeight = round(roiPoints(3)); % - these are just the ROI width and height
roiWidth  = round(roiPoints(4));

handles=guidata(hObject); %_ update all the handles...
handles.partX1=partX1;
handles.partX2=partX2;
handles.partY1=partY1;
handles.partY2=partY2;

handles.roicenterX = (partX1 + round(roiPoints(3))/2);
handles.roicenterY= (partY1 + round(roiPoints(4))/2);

handles.roiHeight = roiHeight;
handles.roiWidth = roiWidth;

current_slice = round(get(handles.Image_Slider,'Value'));
handles.current_slice=current_slice;
particleImage=handles.Image_Sequence_Data(partY1:partY2,partX1:partX2,current_slice);
handles.particleImage=particleImage;

set(handles.RoiSizeDisplay,'String',['Particle ROI is ',' ',num2str(roiHeight),' ', ' by ',num2str(roiWidth)] );

guidata(hObject,handles);  %- at this point we now have the particle in the roi and the size of the roi stored.

我遇到的问题是,当用户第一次点击按钮时,没有任何反应(即根本不会调用正确的线)。当用户再次单击该按钮时,他们可以在图像上绘制一个矩形,但我的程序会抛出错误

Error in imrect (line 83)
            [h_group,draw_api] = imrectAPI(varargin{:});

Error in SemiAutomated_Fionv2p2_5>SetParticleRoiSize_Callback (line 291)
particleroiSize=imrect;% - draw a rectagle around the particle to get a meausr eof ROI size

Error in gui_mainfcn (line 96)
        feval(varargin{:});

Error in SemiAutomated_Fionv2p2_5 (line 56)
    gui_mainfcn(gui_State, varargin{:});

Error in
@(hObject,eventdata)SemiAutomated_Fionv2p2_5('SetParticleRoiSize_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback

我猜错误是因为第一次按键点击没有创建。 有人可以向我解释为什么这个按钮需要点击两次才能工作? (即便如此,它仍然会出错...)

...谢谢 -j

2 个答案:

答案 0 :(得分:0)

看起来像是这个电话:

handles=guidata(hObject); %_ update all the handles...

使用不当;您可能需要将其替换为guidata(hObject,handles)才能实际更新句柄结构。此外,由于您已经在回调结束时调用了guidata(hObject,handles),我可以完全删除此行。

目前正在实施,您:

1)使用handles = guidata(hObject);

获取句柄结构

2)将信息存储在其中

    particleroiSize=imrect;% - draw a rectagle around the particle to get a meausr eof ROI size
    roiPoints=getPosition(particleroiSize); %-get tha parameters fo the rectanlge
    partX1 = round(roiPoints(1));
    partY1 = round(roiPoints(2));
    partX2 = round(partX1 + roiPoints(3));
    partY2 = round(partY1 + roiPoints(4)); % these are the ROi positions in pixels

roiHeight = round(roiPoints(3)); % - these are just the ROI width and height
roiWidth  = round(roiPoints(4));

3)然后再次调用它(handles = guidata(hObject))`。

因此,在您第二次调用以检索之前未更新过的数据时,我猜Matlab不喜欢它。

这可能不是您错误的具体原因,但我认为值得检查。

答案 1 :(得分:0)

我的身材中有3个不同的轴。该程序的第一个版本只有1个轴,所以它工作正常。当我刚刚运行时将其他轴添加到GUI(通过GUIDE)后...我认为它不是选择我想要的轴..我添加了一个父句柄。正确的轴,现在它完美地工作了!