How to create periodic boundary conditions while plotting?

조회 수: 10 (최근 30일)
Liana Shpani
Liana Shpani 2018년 6월 26일
댓글: Liana Shpani 2018년 6월 27일
I want to plot 3D until the z value reaches a certain value (18 in this case), then it should start from z = 0 again. I cut down the intervals in the text file so that when it reaches 18.0 it will go to 0.0, but because I am drawing continuously, there is a line perpendicular to the x-y plane. How can I stop that from happening? I am also attaching the data. Thank you in advance!
contents = fileread('results.txt'); %read whole file at once
contents = strsplit(contents, 'Particle'); %split into cell array at particle
particles = cellfun(@(c) sscanf(c, '%f', [3 Inf])', contents(2:end), 'UniformOutput', false); %scan each section into a 3xN matrix
for i=1:length(particles)
curve=animatedline('linewidth',2,'color',rand(1,3))
for j=1:length(particles{1,i})
x = particles{1,i}(j,1);
y = particles{1,i}(j,2);
z = particles{1,i}(j,3);
figure(1)
addpoints(curve,x,y,z);
drawnow
pause(0.2);
end
end

채택된 답변

Sammit Jain
Sammit Jain 2018년 6월 27일
Hi Liana, what you're trying to do with your data points isn't very evident, since there are no images attached. According to my understanding of the problem, I've made certain assumptions:
  • The kind of plot you end up with, is something like this:
  • Here, you want to avoid the vertical-looking lines, because they're part of a different series of data. What do I mean by series? A sequence of x,y,z values until z reaches 18.
  • These vertical lines are a result of animatedline taking the last data point from the previous series and starting your line from there. (Assuming this is what you meant by 'continuously')
  • The kind of output you're looking for:
(Here, I've just plotted different series with different colors to show that they're separate. You'll notice that the vertical lines are absent)
Solution
Here's your code with some minor changes to keep track of the periodic boundary condition:
contents = fileread('results.txt'); %read whole file at once
contents = strsplit(contents, 'Particle'); %split into cell array at particle
particles = cellfun(@(c) sscanf(c, '%f', [3 Inf])', contents(2:end), 'UniformOutput', false); %scan each section into a 3xN matrix
%for i=1:length(particles) % This wasn't doing anything. Length is 1
view(3); % Changing the plot view to 3D
flag=0; % Keep track of z = 18
curve=animatedline('linewidth',2,'color',rand(1,3));
for j=1:length(particles{1,1})
x = particles{1,1}(j,1);
y = particles{1,1}(j,2);
z = particles{1,1}(j,3);
if(z==18) % If z=18, we want to add THIS point BUT not the one AFTER
flag = j;
end
if j~=flag+1 % Check whether we have reached the point after z=18
addpoints(curve,x,y,z);
drawnow
else % We have reached a new series. Make a new animatedline
curve=animatedline(x,y,z,'linewidth',2,'color',rand(1,3));
% Here, supplying initial points ensures it's a new line.
addpoints(curve,x,y,z);
drawnow
flag=0; % Again set the flag to normal
end
pause(0.2);
end
So in essence, all that needed to be done is re-initializing an animated line with the points from the new sequence, so that it doesn't construct lines from the old points.
Hope this helps.
  댓글 수: 1
Liana Shpani
Liana Shpani 2018년 6월 27일
Thank you so so much!! That was exactly what I was looking for.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by