我正在尝试提高MATLAB图形中动画的绘制和更新速率。我希望通过使用
以下是代码。我将动画编写为一个对象,因为我将把它插入到各种程序中。我定义了类,使用第一个函数构建对象,然后使用第二个函数运行刺激。为了减少速度,我将所有转换和XY数据复制到单独的容器中,以便可以对位置数据执行
创建动画的方法:
这个 GIF 展示了我使用上述代码构建的动画,其中包含5个对象。尽管我使用了 for 循环,但它出现和动画都很快速。
gpuarray
或parfor
循环来更新图形上的许多变量,但显然这不是情况,当这些函数解析时,图形的句柄会失去连接。是否可能有另一种方法来改进和加速动画?我将附上我的代码并简要说明。我还将附上两个GIF,展示我想要做的事情。以下是代码。我将动画编写为一个对象,因为我将把它插入到各种程序中。我定义了类,使用第一个函数构建对象,然后使用第二个函数运行刺激。为了减少速度,我将所有转换和XY数据复制到单独的容器中,以便可以对位置数据执行
cellfun
或其他计算密集型计算,然后覆盖对象的位置数据。classdef StimBud<handle
properties
Window % holds figure
ObjectList % holds object handles
ObjectTransformation % holds object transformation
ObjectLocX
ObjectLocY
ObjectRotation
end
methods
function gh = StimBud()
end
function buildobjects(gh,numofObj)
gh.Window = figure('MenuBar','none', ... % Build Window for stimulus
'Color',[0,0,0]);
for aa = 1:numofObj
gh.ObjectTransformation{aa} = hgtransform; % Add object to end of transformation list
gh.ObjectList{aa} = patch(... % Add object to end of Object list, bind to transformation list
'Parent', gh.ObjectTransformation{aa}, ...
'XData',[0,0,1,1], 'YData',[0,1,1,0],...
'Facecolor', [1,1,0], 'EdgeColor', [1,1,0], ...
'visible','on');
end
% define transforamtion and position variables
parse = 360/numofObj;
rotationcalculation = 0;
XData = repmat(10,1,numofObj);
YData = zeros(1,numofObj);
TmpXdata = cell(numofObj,1); % container to hold all X location data
TmpRot = zeros(numofObj,1); % container to hold the Rotation data
TmpYdata = cell(numofObj,1); % container to hold all Y location data
% Adjust position & rotation of all available objects
for aa = 1:numofObj
% Rotate objects to change direction of movement
gh.ObjectTransformation{aa}.Matrix = makehgtform('zrotate',(deg2rad(aa*parse)+rotationcalculation));
% move object to proper position
gh.ObjectList{aa}.XData = gh.ObjectList{aa}.XData + XData(aa);
gh.ObjectList{aa}.YData = gh.ObjectList{aa}.YData + YData(aa);
TmpXdata{aa} = gh.ObjectList{aa}.XData;
TmpYdata{aa} = gh.ObjectList{aa}.YData;
TmpRot(aa) = (deg2rad(aa*parse)+rotationcalculation);
end
% store variable out of objects for threading
gh.ObjectLocX = TmpXdata;
gh.ObjectLocY = TmpYdata;
gh.ObjectRotation = TmpRot;
end
function RunStim(gh)
figure(gh.Window);
TrialLength = 10; % Length of trial to be run
Framerate = 60;
ObjSpeed = 10;
% pull variables into function
ObjList = gh.ObjectList;
ObjLocX = gh.ObjectLocX;
ObjLocY = gh.ObjectLocY;
ObjRotation = gh.ObjectRotation;
NumofObj = length(ObjList); % Number of Objects in stim system
timer = tic(); % Timer for the stimulus
moveforward = .03*.1*ObjSpeed; % Distance to move in figure
while toc(timer) < TrialLength % Run stimulus through length of project
NextStepX = cellfun(@(x) x+moveforward,ObjLocX);
NextStepY = cellfun(@(x) x+moveforward,ObjLocY);
NextRot = ObjRotation + moveforward;
for aa = 1:NumofObj %% parfor does not work here %%
ObjList{aa}.XData = NextStepX{aa};
end
ObjLocX = NextStepX; % Update X location matrix for next step
ObjLocY = NextStepY; % Update Y location matrix for next step
ObjRotation = NextRot; % Update Rotation matrix for next step
pause(1/Framerate) % Pause window briefly to allow for drawing
end
end
end
end
创建动画的方法:
s = StimBud;
s.buildobjects(5)
s.RunStim
这个 GIF 展示了我使用上述代码构建的动画,其中包含5个对象。尽管我使用了 for 循环,但它出现和动画都很快速。
当我增加对象的数量时(由于for循环,这是非常明显的),动画会明显减慢,不如少量对象的动画那样平滑,并且无法走完相同的距离。
我希望通过多线程来纠正这个问题,但根据我所学,这似乎不是可行的选择。如何在MATLAB图中改善动画效果?我是否考虑错误,不应该使用figure
?