Search code examples
matlabgraphics3dgeometry-surface

Rotating a 3D figure in MATLAB


I generated a 3D surface in matlab.

clear all;
close all;
clc;

x = [1:0.1:5];
y=[1:50];  
[n1 n2] = size(x);
[m1, m2] = size(y);

for i = 1 : m2
    for j = 1 : n2

        z(i,j) = (x(1,j)) / (y(1,i));           
    end
end
[x, y] = meshgrid(x, y);

surf(y, x, z)
colorbar
xlabel('x')
ylabel('y')
zlabel('z')

I got the following plotted surface:

enter image description here

I am interested now to rotate the cube of a quarter turn in the clockwise direction. I know that I can use the "rotate3d on" and choose the best Az and EI, but I didn't understand what are Az and EI and how should be equal to respond to my need?

For example:

enter image description here

There also another function called camroll(). But I don't know what must the value in () in order to rotate the cube of a quarter turn in the clockwise direction. Does 90 degree is the correct answer?

Any help will be very appreciated!


Solution

  • as @ASantosRibeiro mentioned, the rotate function will produce the output you want. As an aside, here is a vectorized version of your code, which you might find useful if you have a significantly larger data set.

    x = 1:0.1:5;
    y=1:50;  
    
    X = repmat(x',1,size(y,2)).';
    Y = repmat(y',1,size(x,2));
    Z = X./Y;
    
    figure
    hSurf = surf(Y,X,Z);
    
    rotate(hSurf,[1,0,0],45) % As proposed by @ASantosRibeiro
    
    colorbar
    xlabel('x')
    ylabel('y')
    zlabel('z')
    

    The repmat function is used to replicate both x and y in order to form X and Y with correct sizes to be allowed to divide one by the other in order to form Z. This operation is quite similar to the call to meshgrid in your code. For a small dataset like the one in your example the running times are similar with both methods (on my laptop), however when I use x = 1:0.1:500 and y = 1:500 for example the vectorized version takes 3x less time, so it might be worthwhile to look at it.

    Hope that helps you!