How to plot lines for variables changing in a loop

조회 수: 1 (최근 30일)
S
S 2020년 4월 7일
댓글: Walter Roberson 2020년 4월 7일
Hi,
I have this data set. I want to make line plots for all the particle trajectories. This is the code so far.
%make structure array file
opt = {'MultipleDelimsAsOne',true};
out = {};
[fid,msg] = fopen('dataset.txt','rt');
assert(fid>=3,msg) %Throw error if condition false
while ~feof(fid) %while loop to repeat when condition is true. feof-read and display one line at a time until you reach the end of the file. ~ Logical NOT
str = fgets(fid); % read line excluding newline character
val = sscanf(str,'time%f'); % read formatted data from strings
if numel(val) %returns the number of elements in array val
hdr = regexp(fgets(fid),'\w+','match'); %returns the starting index of each substring of fgets(fid) that matches the character patterns specified by the regular expression
fmt = repmat('%f',1,numel(hdr)); %Repeat copies of array
tmp = textscan(fid,fmt,opt{:}); %Read formatted data from text file or string
tmp = cell2struct(tmp,hdr,2); %Convert cell array to structure array - structArray = cell2struct(cellArray, fields, dim)
tmp.time = val;
out{end+1} = tmp; %{end+1} assigning a value to the next position, which automatically extends the array to fit that size.
end
end
fclose(fid);
out = [out{:}]; % All structures must have the same fields!
%----------------------------------------------------------------------------------------
% Make trajectory plots.
figure; axis square; hold on;
set(gca,'XLim',[-0.02 0.02], 'YLim', [-0.02 0.02]);
numsteps=4;
for frameNr = 1:numsteps
cla;
for i=1:50
plot(out(frameNr).x(i),out(frameNr).y(i),'*');
end
%lineplot
%?
% Get the frame for the animation.
frames(frameNr) = getframe;
end
How can I plot lines for each particle trajectory here. Thank you very much.
  댓글 수: 3
S
S 2020년 4월 7일
Hi,
How can I add 50 animated line?
Thanks
Walter Roberson
Walter Roberson 2020년 4월 7일
%initialize
for i = 1 : 50; AL(i) = animatedline(nan,nan, '-*'); end
for i=1:50
addpoints(AL(i), out(frameNr).x(i), out(frameNr).y(i));
end
drawnow()

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by