Hasan
Hasan

Reputation: 37

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'. Error in ==> ImGui>Browse_Callback at 19 axes(handles.axes1) ??? Error while evaluating uicontrol Callback

I have tried working with both predefined axes [like 'axes(handles.axes1);'] as well as with post-defined [like '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

Upvotes: 0

Views: 1399

Answers (1)

Hoki
Hoki

Reputation: 11802

Use the guidata function.
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). Then when you GUI is fully defined, call guidata to store this handle structure in a place where any callback can access it.

Then in your callback function, call guidata again to retrieve this handle structure and get access to your objects (you axes and your textbox).

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.

Upvotes: 1

Related Questions