how to plot 3d using patch?

72 views (last 30 days)
Sierra
Sierra on 27 May 2022
Commented: Sierra on 11 Jun 2022
i have a example code. but i don't know how to adapt this code to my data.
my data is longitude, latitude, altitude data(aircraft data).
i drew myself what i want to plot for you conveniene.(first image and second image) % ignore red point in box
first image is a cross section of trajectory. and I want to make somethig like a tube with this data.
and i have to make a box using 'percentile' from cross section and connect i th box's vertices to i+1 th box's vertices, so i can make something like a tube(second image).
I annotated what i don't understand on example code.
let me know which code do i need to change for my output!
thanks!
% Example Code
x = rand(1,5000)*10+125;
y = randn(1,5000)*50+250;
z = randn(1,5000)*150+300;
t = linspace(0, 1, 5000); % why is 't' needed?
x = x + sin(2.5*pi*t)*125;
y = y + cos(1.5*pi*t)*125;
yv = linspace(min(y), max(y), 7); % Set 'Y' Values For The Box Locations
figure
scatter3(x,y,z,'.')
hold on
for k = 1:numel(yv)
yrng = find(y>=0.8*yv(k) & y <=1.2*yv(k)); % what do 0.8 and 1.2 mean? and why do i need to use this code?
xpctl = prctile(x(yrng),[2.5 97.5]);
zpctl = prctile(z(yrng),[2.5 97.5]);
xl(k,:) = xpctl;
zl(k,:) = zpctl;
patch([xpctl flip(xpctl)], [0 1 1 0]+yv(k), [[1 1]*zpctl(1) [1 1]*zpctl(2)], 'r', 'FaceAlpha',0.25)
% I don't know why fllip 'xptcl' and why plus yv(K)
end
plot3(xl(:,1), yv(:), zl(:,1), '-k', 'LineWidth',2)
plot3(xl(:,1), yv(:), zl(:,2), '-k', 'LineWidth',2)
plot3(xl(:,2), yv(:), zl(:,1), '-k', 'LineWidth',2)
plot3(xl(:,2), yv(:), zl(:,2), '-k', 'LineWidth',2)
hold off
xlabel('X')
ylabel('Y')
zlabel('Z')
% xlim([120 140])
% ylim([100 400])
view(45,30)
% My code
for i = 1:length(33L)
x = 33L(i).Longitude;
y = 33L(i).Latitude;
z = 33L(i).BAlt;
t = linspace(0, 1, 5000);
yv = linspace(min(y), max(y), 30); % Set 'Y' Values For The Box Locations
scatter(x,y,z,'.')
hold on
end
for k = 1:numel(yv)
yrng = find(y>=0.8*yv(k) & y <=1.2*yv(k));
xpctl = prctile(x(yrng),[2.5 97.5]);
zpctl = prctile(z(yrng),[2.5 97.5]);
xl(k,:) = xpctl;
zl(k,:) = zpctl;
patch([xpctl flip(xpctl)], [0 1 1 0]+yv(k), [[1 1]*zpctl(1) [1 1]*zpctl(2)], 'r', 'FaceAlpha',0.25)
end
plot3(xl(:,1), yv(:), zl(:,1), '-k', 'LineWidth',2)
plot3(xl(:,1), yv(:), zl(:,2), '-k', 'LineWidth',2)
plot3(xl(:,2), yv(:), zl(:,1), '-k', 'LineWidth',2)
plot3(xl(:,2), yv(:), zl(:,2), '-k', 'LineWidth',2)
hold off
xlabel('X')
ylabel('Y')
zlabel('Z')
  2 Comments
KSSV
KSSV on 27 May 2022
Does code run in the first place?
for i = 1:length(33L)
x = 33L(i).Longitude;
y = 33L(i).Latitude;
z = 33L(i).BAlt;
The above lines violate MATLAB rules.
Sierra
Sierra on 27 May 2022
My bad. I used scatter3 and It wokred.

Sign in to comment.

Accepted Answer

Prahlad Gowtham Katte
Prahlad Gowtham Katte on 8 Jun 2022
Hello,
As per my understanding of the query ,you want to use the example code for plotting a 3d graph using patch function but wants to know why certain things in the code is used.
Firstly “linspace” function gives equidistant numbers within specified range which is used later in the code for getting sample points of x and y. “Find ”function is used for finding the indices of the array which satisfy a specified condition.It is used in the example code for getting indices of y values which is greater than 80% of y and less than 120% of y. These are later used to get the percentile required for using “patch” function.
Flip function is used to invert the order of x percentile array in order to get a rectangular surface. We add “yv(k)” to the array because we need to get the box around y=yv(k).Thus we add “yv(k)” to the initial array.
For more information on the functions used in the code please refer to the following links.
Hope it helps!
  1 Comment
Sierra
Sierra on 11 Jun 2022
Hello Parhlad. Thanks for your answering.
I uploaded a new question associated with this question.
If you have time, could you answer my new question?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!