简体   繁体   English

Matlab在gui中显示图像

[英]Matlab show image in gui

I have been trying to show an image after browsing it. 我一直在尝试浏览后显示图像。 However, I have been getting errors like: 但是,我一直得到类似以下错误:

??? ??? Reference to non-existent field 'axes1'. 引用不存在的字段“ axes1”。 Error in ==> ImGui>Browse_Callback at 19 axes(handles.axes1) ??? 错误==> ImGui> 19根轴上的浏览回调(handles.axes1)??? Error while evaluating uicontrol Callback 评估uicontrol回调时出错

I have tried working with both predefined axes [like 'axes(handles.axes1);'] as well as with post-defined [like 'imshow(imgorg, 'Parent', handles.axes1);']. 我曾尝试使用两个预定义的轴[例如'axes(handles.axes1);']和后期定义的[例如'imshow(imgorg,'Parent',handles.axes1);']。 Unfortunately, both techniques have not worked out for me and I am consistently stuck with axes. 不幸的是,这两种技术对我来说都没有奏效,而且我始终坚持使用轴。 I also tried making a customized axes and work with that but it also failed to show my image on the figure. 我也尝试制作一个自定义轴并使用它,但是它也未能在图中显示我的图像。 Can anyone please identify/ rectify the problem in my code: 任何人都可以在我的代码中识别/纠正该问题:

function ImGui
    f =figure('Visible','on','Position',[460,200,700,385]);
    BrowseBt = uicontrol('Style','pushbutton',...
                 'String','Browse','Position',[600,350,70,25],...
                 'Callback',@Browse_Callback);
    dispnames  = uicontrol('Style','text','String','',...
           'Position',[50,350,400,20]);
    movegui(f,'center');

    function Browse_Callback(hObject, eventdata, handles) 

        handles.output = hObject;
       [FileName,PathName] = uigetfile('*.jpg;*.png','Select an image file',...
                                    'C:\Users\owner\Downloads\Conjunctiva\SGRH');

       fpname = strcat(PathName,FileName);
       dispnames  = uicontrol('Style','text','String',fpname,...
           'Position',[50,350,400,20]);
       imgorg = imread(fpname);

       handles.output = hObject;
       guidata(hObject, handles);
       axes(handles.axes1);
       imshow(imgorg);
%        ImAxes = axes('Parent', f, ...
%                  'Units', 'normalized', ...
%                  'position',[50 50 400 250]);
%              'HandleVisibility','callback', ...
%        imshow(imgorg, 'Parent', handles.axes1);
%        imshow(imgorg, 'Parent', handles.ImAxes);
    end
end

Use the guidata function. 使用guidata函数。
and reorganize your code a little bit 并重新整理一下代码

You define all your uicontrols (button, textbox, axes etc ...) and you assign their handle to a structure (called handles here). 您定义所有的uicontrols(按钮,文本框,轴等),然后将它们的句柄分配给结构(在此称为handles )。 Then when you GUI is fully defined, call guidata to store this handle structure in a place where any callback can access it. 然后,在完全定义GUI时,调用guidata将此句柄结构存储在任何回调都可以访问它的地方。

Then in your callback function, call guidata again to retrieve this handle structure and get access to your objects (you axes and your textbox). 然后,在回调函数中,再次调用guidata来检索此句柄结构并访问对象(轴和文本框)。

function ImGui
    f =figure('Visible','on','Position',[460,200,700,385]);
    handles.BrowseBt = uicontrol('Style','pushbutton',...
                 'String','Browse','Position',[600,350,70,25],...
                 'Callback',@Browse_Callback);
    handles.dispnames  = uicontrol('Style','text','String','',...
           'Position',[50,350,400,20]);
    handles.ImAxes = axes('Parent', f, ...
             'Units', 'pixels', ...
             'position',[30 30 640 300],...
             'visible','off');
    movegui(f,'center');
    guidata(f,handles) ;

    function Browse_Callback(hObject, eventdata) 
       handles = guidata(hObject);

       [FileName,PathName] = uigetfile('*.jpg;*.png','Select an image file');
       fpname = strcat(PathName,FileName);

       imgorg = imread(fpname);

       set(handles.dispnames,'String',FileName)
       set(handles.ImAxes,'visible','on') ;
       imshow(imgorg, 'Parent', handles.ImAxes);
       guidata(hObject, handles);
    end
end

In this specific case you don't really need to call guidata again at the end of the callback to store the values again but it is good practice, in case you modified something you want the changes to be saved. 在这种特定情况下,您实际上并不需要在回调结束时再次调用guidata再次存储值,但这是一个很好的做法,以防万一您修改了一些想要保存的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM