Search code examples
matlabanimationmatlab-figure

linkprop objects with hgtransform


Literately figured out the last problem as soon as I posted. Was able to fix many of the issues I was having. Now the problem revolves around hgtransform and linkprop. How does one copy the object location and transformation to additional figures. The code below will copy objects from the first axes to the next and animate making all of them move. However it doesn't copy the transformation.

fig = figure();
% create subplots for stim system 3 plate setup
for aa = 1:3
    Stimsubfigures{aa} = axes(...
      'Position',[((aa*.21)-.2),.2,.2,.2],'color','none');
    set(Stimsubfigures{aa},'xLim',[-320,320])
    set(Stimsubfigures{aa},'YLim',[-240,240])
    set(Stimsubfigures{aa},'Visible','off')
end

axes(Stimsubfigures{1});
for aa = 1:10
    Xdata = [1+aa*50,10+aa*50,10+aa*50,1+aa*50];
    ObjectTransformation{aa,1} = hgtransform;                   % Add object to end of transformation list
    ObjectList{aa,1} = patch(...                                % Add object to end of Object list, bind to transformation list
                'Parent', ObjectTransformation{aa}, ...   
                'XData',Xdata, 'YData',[1,1,20,20],...
                'Facecolor', [1,0,0], 'EdgeColor', [1,0,0], ...
                'visible','on'); 
    ObjectTransformation{aa,1}.Matrix = makehgtform('zrotate',50);
    NextStepX{aa,1} = Xdata;        
end

tmp = transpose([ObjectList{:}]);
tmptrans = transpose([ObjectTransformation{:}]);

TrialLength = 10;

 % copy objects to other figures
copyobj(tmp,Stimsubfigures{2})
copyobj(tmp,Stimsubfigures{3})
property_names = {'XData', 'YData', 'ZData'};
for aa = 1:10
linked_objects = [tmp(aa),...
                          Stimsubfigures{2}.Children(aa),...
                          Stimsubfigures{3}.Children(aa)];
hlink{aa} = linkprop(linked_objects,property_names);
end

timer = tic(); 

while true
    t1 = toc(timer);
    if t1 >= TrialLength, break;end                             % break loop once time trial ends
    NextStepX = cellfun(@(x) x+1,NextStepX,'UniformOutput',false);
    [tmp.XData] = NextStepX{:};
    drawnow;
    pause(0.1);
    step = NextStepX;
end

for aa = 1:3
    delete(Stimsubfigures{aa}.Children)
end

When I change this section to copy the transformation, the objects transform correctly but lose the animation.

 % copy objects to other figures
copyobj(tmptrans,Stimsubfigures{2})
copyobj(tmptrans,Stimsubfigures{3})

property_names = {'XData', 'YData', 'ZData'};
trans_names = {'zrotate'};

for aa = 1:10
linked_objects = [tmp(aa),...
                          Stimsubfigures{2}.Children(aa),...
                          Stimsubfigures{3}.Children(aa)];
Trlink_objects = [tmptrans(aa),...
                          Stimsubfigures{2}.Children(aa),...
                          Stimsubfigures{3}.Children(aa)];
hlink{aa} = linkprop(linked_objects,trans_names);
Trhlink{aa} = linkprop(Trlink_objects,trans_names);
end

I tried to perform an copyobj on both handles but it just results in two sets of objects. How can one link all three together so I can perform rotation change Xdata?


Solution

  • Figured it out. Will post answer for those who had similar questions.

    The hgtransform is a parent of the objects which are being rotated. because of this when I copy the hgtransform, the children are also copied, hence why the objects appear in the proper orientation in the other windows. From here, I need to link the children from the copied parents to generate the animation.

    % copy objects to other figures
    copyobj(tmptrans,Stimsubfigures{2})
    copyobj(tmptrans,Stimsubfigures{3})
    
    property_names = {'XData', 'YData', 'ZData'};
    
    for aa = 1:10
        linked_objects = [tmptrans(aa).Children(1),...
                          Stimsubfigures{2}.Children(aa).Children(1),...
                          Stimsubfigures{3}.Children(aa).Children(1)];
    
        hlink{aa} = linkprop(linked_objects,trans_names);
    end
    

    By replacing the part of code with the section above. One can transform the objects and animate.