필터 지우기
필터 지우기

Question on Plotting from Cell Array

조회 수: 1 (최근 30일)
Gesiren Zhang
Gesiren Zhang 2019년 8월 24일
답변: the cyclist 2019년 8월 24일
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

채택된 답변

the cyclist
the cyclist 2019년 8월 24일
% 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)

추가 답변 (2개)

Matt J
Matt J 2019년 8월 24일
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

dpb
dpb 2019년 8월 24일
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.

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by