MATLAB: Legend for plot3 command

3d plotsSimulink 3D Animation

I am going to plot two 3D data with the help of below command lines.
How can I insert legend for both the 3D data in a single window?
plot3(x,y,z);
hold on;
plot3(xx,yy,zz);

Best Answer

Use the DisplayName property of graphics objects to specify the legend string.
plot3(x,y,z, 'DisplayName', 'Object1');
hold on;
plot3(xx,yy,zz, 'DisplayName', 'Object2');
legend()
or
p1 = plot3(x,y,z, 'DisplayName', 'Object1');
hold on;
p2 = plot3(xx,yy,zz, 'DisplayName', 'Object2');
legend([p1,p2])