# How to create 3D plot in Matlab? How to set up the Matrix of the points?

3 views (last 30 days)
Katharine Woodruff on 25 Jul 2019
Answered: Ganesh Regoti on 1 Aug 2019
I have the following code that is using a matrix called "permutation" size 50x2 and feeds the first column value as "rollang" and the second column value as "pitchang" into equations to output the "distance" value. Currently, the corresponding three points are output into the pointsRPS matrix, size 80x3. When I go to surface plot, its completely wrong. Currently, it outputs like this:
X Y Z
30 0 600
40 0 450
50 0 360
...
But I need it to output like this with X values along the top and Y values alon the sie, with the Z point inside:
0 30 40 50
0 600 450 360
10 600 450 360
20 .... .... ...
i.e. the X values don't repeat and the Y values don't repeat.
AKA I want it to look like this but un matrix form in matlab... #### CODE #########
pointsRPS = [];
for i =1:size(permutation, 1)
rollang = permutation(i,1);
rollTh = 30;
pitchTh = 30;
pitchang= permutation(i,2);
if rollang >= rollTh && pitchang >= pitchTh
distance = (((180/rollang) + (90/pitchang))/2)*100;
pointsRPS = [pointsRPS; rollang pitchang distance];
elseif rollang >= rollTh
distance = (180/rollang)*100;
pointsRPS = [pointsRPS ; rollang pitchang distance];
elseif pitchang >= pitchTh
distance = (90/pitchang)*100;
pointsRPS = [pointsRPS; rollang pitchang distance];
else
distance = 0;
end
end
disp(pointsRPS)
surfaceplotTest = surf(rollang, pitchang, pointsRPS)
title('Pitch and Roll Angle Surface Plot')
xlabel( 'Roll Angle')
ylabel('Pitch Angle')
zlabel('Distance')

Ganesh Regoti on 1 Aug 2019
meshgrid function will work for your case. I have presumed the data and tried the following code and it works fine.
permutation = randi(202,50,2);
a = sort(unique(permutation(:,1)));
b = sort(unique(permutation(:,2)));
[X,Y] = meshgrid(a,b);
pointsRPS = zeros(size(X)); %Create pointsRPS to store the distance
for i =1:size(X, 1)
for j = 1:size(X,2)
rollang = X(i,j);
rollTh = 30;
pitchTh = 30;
pitchang= Y(i,j);
if rollang >= rollTh && pitchang >= pitchTh
distance = (((180/rollang) + (90/pitchang))/2)*100;
pointsRPS(i,j) = distance;
elseif rollang >= rollTh
distance = (180/rollang)*100;
pointsRPS(i,j) = distance;
elseif pitchang >= pitchTh
distance = (90/pitchang)*100;
pointsRPS(i,j) = distance;
else
distance = 0;
pointsRPS(i,j) = distance;
end
end
end
surfaceplotTest = surf(X, Y, pointsRPS);
title('Pitch and Roll Angle Surface Plot')
xlabel( 'Roll Angle')
ylabel('Pitch Angle')
zlabel('Distance')