How to plot lines for variables changing in a loop
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!