保存Matlab图形并保留所有手柄 [英] Saving a matlab figure and keeping all handles

查看:91
本文介绍了保存Matlab图形并保留所有手柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MATLAB中生成一个带有弹出菜单的图形,该菜单会更改子图的轴.到目前为止,该方法有效.但是当使用文件">保存"保存图形时,我的句柄被删除,并显示以下错误消息:

I am trying to produce a figure in MATLAB with a popupmenu that changes the axes of a subplot. This works so far. But when saving the figure using File > Save, my handles get deleted and it shows me the following error message:

Error using struct2handle
Error while evaluating uicontrol CreateFcn

Error using handle.handle/set
Invalid or deleted object.

Error in popup_test/mimi (line 33)
    set(h1,'XData', [0,8],'YData',[0,8])

Error while evaluating uicontrol Callback

在保存图形时,似乎删除了句柄h1.它仍然是一个数字,但ishandle(h1)返回0.

It appeares, that when saving the figure, the handle h1 is deleted. It still is there as a number but ishandle(h1) returns 0.

这是我用以下方法生成的图形的代码:

This is the code I have produced my figure with:

function popup_test2
figure;
a=magic(4);
h1=imagesc(a);
uicontrol(...
    'Style', 'popup',...
    'String', 'first|second',...
    'Position', [20 340 100 50],...
    'Callback', @popupfcn,...
    'CreateFcn', @popupfcn);

    function popupfcn(hObj,event) %#ok<INUSD>
        % Called when user activates popup menu
        val = get(hObj,'Value');
        if val ==1            
            set(h1,'XData', [0,5],'YData',[0,5])
        elseif val == 2
            set(h1,'XData', [0,8],'YData',[0,8])
        end
    end

end

到目前为止,我已经尝试使用saveas(gcf,'filename.fig')(不起作用)和hgsave进行保存,这听起来很有希望,但是我不知道如何正确使用它...

So far I have tried saving using saveas(gcf,'filename.fig') (which didn't work) and hgsave, which sounded promising, but I didn't know how to use it correctly...

推荐答案

您所缺少的是在加载图形后重新创建h1的方法.可以使用以下代码行完成此操作:

What you're missing is recreating h1 after loading the figure. This can be done using the following line of code:

h1 = findobj(gcf,'type','image');

findobj 查找绘制图像的句柄-允许您可以根据需要进行更改.

findobj finds the handle of the plotted image- allowing you to change it as you like.

查看最终代码:

function popup_test2
figure;
a=magic(4);
h1=imagesc(a);
uicontrol(...
    'Style', 'popup',...
    'String', 'first|second',...
    'Position', [20 340 100 50],...
    'Callback', @popupfcn,...
    'CreateFcn', @popupfcn);

    function popupfcn(hObj,event) %#ok<INUSD>
        h1=findobj(gcf,'type','image');
        % Called when user activates popup menu
        val = get(hObj,'Value');
        if val ==1            
            set(h1,'XData', [0,5],'YData',[0,5])
        elseif val == 2
            set(h1,'XData', [0,8],'YData',[0,8])
        end
    end

end

请注意,通常应使用 guidata .

Please note that saving data/handles along with your figure should generally be done using guidata.

这篇关于保存Matlab图形并保留所有手柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆