[英]How to pass handles from one function to another
当我运行我的代码时,会有两个数字,一个名为“图 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);
退出循环后我会得到这个错误
错误使用 imshow>validateParent(第 352 行) HAX 必须是有效的轴句柄。 imshow 中的错误(第 257 行) validateParent(specific_args.Parent) Main 中的错误(第 297 行) imshow(image,'Parent',handles.axes1);
*ps:我实际上不知道为什么“图 1”会存在。
在检查您的问题时,我不确定“guihandles”是否覆盖了轴手柄; 在重新创建您的问题的示例中,它没有。 此外,保存手柄时轴是否存在?
请尝试以下修改后的代码是否有效。
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
我也遇到了这个问题,所以我将描述我想出的快速解决方案。 在您知道 handles 正在按照您期望的方式执行的函数中(在我的情况下是OpeningFcn
),我会将句柄保存为应用程序数据:
setappdata(0,'handles',handles);
然后在句柄不代表任何轴的函数中,我会调用:
handles = getappdata(0,'handles');
这样做然后允许我访问我定义的轴,例如handles.axes1
。
我在尝试在参数更改后实现生成的图像时遇到了同样的问题。 在我的例子中,我使用了多个图形,我想在按下按钮后在最后一个图形的轴上显示结果:
ok_mask_button= uicontrol(handles.pan,'Style','pushbutton','Units', 'normal',
'Position',[.55 .9 .08 .06],'FontSize',12,'FontWeight','bold',
'String','Ok','Callback', @Mask_parameters);
所以要使用我的回调函数显示图像,解决方案是:
% some code above%
...
ax = gca;
imshow(handles.mask,'Parent',ax)
我需要使用 gca 来指定我想在当前轴上工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.