Gregor Isack
Gregor Isack

Reputation: 1121

How to pass handles from one function to another

When i run my code, there'll be two figures, one is names as 'Figure 1', another 'test'. What i'm trying to do is let the imshow shows on the 'test' not 'Figure 1'.

himg=GUI('name','test','NumberTitle','off'); %where GUI was designed using GUIDE
handles = guihandles(himg);
 while ishandle(himg)
    if sum(depthMetaData.IsSkeletonTracked)>0
      util_skeletonViewer(skeletonJoints,image,1,handles); %refer code below
    else
      imshow(image,'Parent',handles.axes1);
    end
 end

function [] = util_skeletonViewer(skeleton, image, nSkeleton,handles)
imshow(image,'Parent',handles.axes1);

and I'll get this error after exiting the loop

Error using imshow>validateParent (line 352) HAX must be a valid axes handle. Error in imshow (line 257) validateParent(specific_args.Parent) Error in Main (line 297) imshow(image,'Parent',handles.axes1);

*ps: i don't actually know why 'Figure 1' will even exist though.

Upvotes: 1

Views: 747

Answers (3)

Ahmed
Ahmed

Reputation: 121

I faced the same problem trying to actualise the resulting image after parameter changes. In my case I used more than one figure, and I wanted to display the result on axes of my last figure after a pushbutton:

ok_mask_button= uicontrol(handles.pan,'Style','pushbutton','Units', 'normal', 
'Position',[.55 .9 .08 .06],'FontSize',12,'FontWeight','bold', 
'String','Ok','Callback', @Mask_parameters);

So to display the image using my callback function, the solution was:

   % some code above%
    ...
    ax = gca;
    imshow(handles.mask,'Parent',ax)

I needed to use gca to specify that I wanted to work on the current axes.

Upvotes: 0

John Bale
John Bale

Reputation: 443

I was also having this issue so I will describe my quick fix I came up with. In a function you know handles is performing as you'd expect (in my case in OpeningFcn) I would save the handles as app data:

setappdata(0,'handles',handles);

Then in the function where handles wasn't representing any axes I would call:

handles = getappdata(0,'handles');

Doing this then allowed me to access my defined axes, e.g handles.axes1.

Upvotes: 0

Chris
Chris

Reputation: 62

In checking your problem, I am not sure whether 'guihandles' covers the axis handle; in an example to recreate your problem it did not. Additionally, is the axis even existing when you save the handles?

Please try whether the following adapted code works.

himg=GUI('name','test','NumberTitle','off'); %where GUI was designed using GUIDE
handles = guihandles(himg);
handles.axis1 = [];
while ishandle(himg)
    if sum(depthMetaData.IsSkeletonTracked)>0
        util_skeletonViewer(skeletonJoints,image,1,handles); %refer code below
    else
        if ~isempty(handles.axes1)
            imshow(image,'Parent',handles.axes1);
        else
            figure(himg)
            imshow(image,'Parent',gca);
            handles.axes1 = gca;
        end
    end
end

function [] = util_skeletonViewer(skeleton, image, nSkeleton,handles)
    if ~isempty(handles.axes1)
        imshow(image,'Parent',handles.axes1);
    else
        figure(himg)
        imshow(image,'Parent',gca);
        handles.axes1 = gca;
    end

Upvotes: 1

Related Questions