MATLAB Answers

Question on Plotting from Cell Array

7 views (last 30 days)
Gesiren Zhang
Gesiren Zhang on 24 Aug 2019
Answered: the cyclist on 24 Aug 2019
Hi guys,
I have a 1000*10 cell array, where each cell is a 1*3 3D coordinate. A segment of this cell array is shown below:
The rows represent time in second, and each column represent location of an object, so each cell is an object's coordinate in space at a given second. There are 10 columns for 10 objects, and 1000 rows for 1000 seconds. I wish to create an animation of all these objects' locations in space from 1s to 1000s: how would I do that? The objects can just be scatter points in a MATLAB 3D plot. Thank you for your help!
GZ

  0 Comments

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 24 Aug 2019
% Parameters
NCOORD = 3;
NT = 1000;
NOBJ = 10;
MARKERSIZE = 72;
% Pretend data
C = cell(NT,NOBJ);
C = cellfun(@(x)rand(1,NCOORD),C,'UniformOutput',false);
% Format into numerical array for convenience
coordByTimeByObject = reshape([C{:}],NCOORD,NT,NOBJ);
% Permute for plotting convenience
timeByObjectByCoord = permute(coordByTimeByObject,[2 3 1]);
% Preallocate the movie frames
M(NT).cdata = [];
M(NT).colormap = [];
% Plot each time step
figure
for nt = 1:NT
scatter3(timeByObjectByCoord(nt,:,1),timeByObjectByCoord(nt,:,2),timeByObjectByCoord(nt,:,3),MARKERSIZE,1:NOBJ);
title(sprintf('Time point: %4d',nt))
xlim([0 1])
ylim([0 1])
zlim([0 1])
% Capture the movie frame
M(nt) = getframe(gcf);
clf
end
% Write the video file
v = VideoWriter('Watch the objects move.mp4','MPEG-4');
open(v)
writeVideo(v,M)
close(v)

  0 Comments

Sign in to comment.

More Answers (2)

Matt J
Matt J on 24 Aug 2019
Points=reshape( [CellArray{:}] ,3,[],10); %
for i=1:1000
X=Points(1,i,:);
Y=Points(2,i,:);
Z=Points(3,i,:);
scatter3(X(:),Y(:),Z(:)); drawnow
end

  0 Comments

Sign in to comment.


dpb
dpb on 24 Aug 2019
Dereference the cell array with the curlies "{}"
for i=1:size(CA,2)
scatter3(CA{i}(:,1),CA{i}(:,2),CA{i}(:,3))
if i==1,hold on, end
end
if the cell array is CA the above will put all on a single axis with cycled colors. Insert additional arguments for size or varied color map or whatever--"salt to suit".
Or, could create subplots of additional figure each pass, all just depends on the effect you're after.

  0 Comments

Sign in to comment.

Sign in to answer this question.