简体   繁体   中英

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.

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

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, eg handles.axes1 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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