Question on Plotting from Cell Array
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Gesiren Zhang
le 24 Août 2019
Réponse apportée : the cyclist
le 24 Août 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:![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/235189/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/235189/image.png)
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 commentaires
Réponse acceptée
the cyclist
le 24 Août 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 commentaires
Plus de réponses (2)
Matt J
le 24 Août 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 commentaires
dpb
le 24 Août 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 commentaires
Voir également
Catégories
En savoir plus sur Animation dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!