我如何从情节处理程序中绘图?

时间:2012-12-10 11:26:34

标签: matlab

我有情节的处理程序或图的句柄 例如:

h = plot([1:0.2:10])
xx=get(h)
xx = 

           DisplayName: ''
            Annotation: [1x1 handle]
                 Color: [0 0 1]
             LineStyle: '-'
             LineWidth: 0.5000
                Marker: 'none'
            MarkerSize: 6
       MarkerEdgeColor: 'auto'
       MarkerFaceColor: 'none'
                 XData: [1x46 double]
                 YData: [1x46 double]
                 ZData: [1x0 double]
          BeingDeleted: 'off'
         ButtonDownFcn: []
              Children: [0x1 double]
              Clipping: 'on'
             CreateFcn: []
             DeleteFcn: []
            BusyAction: 'queue'
      HandleVisibility: 'on'
               HitTest: 'on'
         Interruptible: 'on'
              Selected: 'off'
    SelectionHighlight: 'on'
                   Tag: ''
                  Type: 'line'
         UIContextMenu: []
              UserData: []
               Visible: 'on'
                Parent: 173.0107
             XDataMode: 'auto'
           XDataSource: ''
           YDataSource: ''
           ZDataSource: ''

此处理程序包含所有绘图信息,如何再次绘制?这是一个使用plot的简单示例,但它也适用于slice

3 个答案:

答案 0 :(得分:6)

如果我理解你的问题,你想使用结构xx重现一个情节。 提供的答案是在正确的轨道上,但这是实现您想要的更短的方式:

figure
h2 = plot(0);
ro_props = [fieldnames(rmfield(xx, fieldnames(set(h2)))); 'Parent'];
xx = rmfield(xx, ro_props);
set(h2, xx)

最后一个set命令使用struct xx来设置所有值并重现您的绘图。请注意,在调用ro_props之前,xx会删除只读属性set

编辑:修改后的答案,根据this suggestion自动检测只读属性。

答案 1 :(得分:5)

您可以使用copyobj

h = plot([1:0.2:10])
xx=get(h)
figure
copyobj(h,gca)

这会将情节复制到新图上

请参阅:http://www.mathworks.com/help/matlab/ref/copyobj.html

更新

我认为你不能直接从结构xx创建,试图这样做:

h = plot([1:0.2:10])
xx=get(h)

h2 = plot(0,0)
set(h2,xx)

引发错误

Error using graph2d.lineseries/set
Changing the 'Annotation' property of line is not allowed.

您需要手动设置一些属性值,如下所示:

h = plot([1:0.2:10])
xx=get(h)


figure
h2 = plot(0.0)

names = fieldnames(xx);

fieldCount = size(names,1);

protectedNames = {'DisplayName' 'Annotation' 'BeingDeleted' 'Type' 'Parent'}

for i = 1:fieldCount
    name = names{i};
    if ( ismember(protectedNames, name) == false  )


        set(h2, name, getfield(xx,name))

    end
end

yy=get(h2)

答案 2 :(得分:0)

我不知道是否有更简单的方法,但你在XData和YData中有x,y值。

执行:

figure
plot(get(h,'XData'),get(h,'YData'))