Search code examples
matlabgridplotcube

How to plot 3D grid (cube) in Matlab


Hi I would like to plot transparent cube-shaped grid with lines in it. Something like this: enter image description here

However, I managed only to draw a 2D grid:

[X,Y] = meshgrid(-8:.5:8);
Z = X+1;
surf(X,Y,Z)

I use Matlab R2009b. If it is impossible to plot this in matlab could you recommend me a software I could use.


Solution

  • If you don't mind a few for loops, something like this will work:

    clf
    figure(1)
    for g = 0:.2:2
    for i = 0:.2:2
    
       plot3([g g], [0 2], [i, i])
       hold on
    end
    end
    
    for g = 0:.2:2
    for i = 0:.2:2
    
       plot3([0 2], [g g], [i, i])
       hold on
    end
    end
    
    for g = 0:.2:2
    for i = 0:.2:2
    
       plot3([i i], [g g], [0 2])
       hold on
    end
    end
    

    You will just need to make the grid transparent by probably changing line properties, I don't think you can change alpha values to accomplish this. Hope that is helpful.