简体   繁体   English

如何将句柄从一个函数传递到另一个函数

[英]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'.当我运行我的代码时,会有两个数字,一个名为“图 1”,另一个名为“测试”。 What i'm trying to do is let the imshow shows on the 'test' not 'Figure 1'.我想要做的是让 imshow 在“测试”而不是“图 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.错误使用 imshow>validateParent(第 352 行) HAX 必须是有效的轴句柄。 Error in imshow (line 257) validateParent(specific_args.Parent) Error in Main (line 297) imshow(image,'Parent',handles.axes1); imshow 中的错误(第 257 行) validateParent(specific_args.Parent) Main 中的错误(第 297 行) imshow(image,'Parent',handles.axes1);

*ps: i don't actually know why 'Figure 1' will even exist though. *ps:我实际上不知道为什么“图 1”会存在。

In checking your problem, I am not sure whether 'guihandles' covers the axis handle;在检查您的问题时,我不确定“guihandles”是否覆盖了轴手柄; 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:在您知道 handles 正在按照您期望的方式执行的函数中(在我的情况下是OpeningFcn ),我会将句柄保存为应用程序数据:

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 .这样做然后允许我访问我定义的轴,例如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.我需要使用 gca 来指定我想在当前轴上工作。

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

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