I wanted to play with this as a building block for a bigger project. (These projects keep me entertained!)
I wanted to write a bit of MATLAB code that would create a colorful rotating cylinder. I know that core MATLAB offers the function cylinder that can generate surfaces of revolution (and thus cylinders!) However, I do not particularly like to be spoon-fed code all the time. Especially when it is straight forward to use a bit of math and code a few extra lines.
Here it goes:
% Define number of points on the x and z axis
N = 64;
M = 32;
x = linspace(-pi, pi, N);
z = linspace(-2, 2, M);
% Create color assignment for each data point.
% Cdata is a property of function surf that defines the vertex colors. A
% matrix containing values that specify the color at every point in ZData.
% Use pseudo-random number generator for ‘unique’ pattern’. Define outside loop to maintain assignment of colors for all iterations.
coloring = rand(M,N+1);
% Code in for-loop to generate rotational motion.
for ii = 1:N
dummy = [ii:N 1:ii];
x_ = x(dummy);
[xx, zz] = meshgrid(x_,z);
% Use parametric equations
X = 1 .*cos(xx);
Y = 1 .*sin(xx);
Z = zz;
h = surf(X,Y,Z); % Create 3D surface plot
axis off
set(h,'CData',coloring) % Use color scheme defined outside of loop
shading interp
F(ii) = getframe; % Get movie frames
pause(0.1) % To observe motion
end
An AVI movie can be created by playing the movie frames stored in memory:
movie2avi(F, 'rotatingCylinder01.avi', 'fps', 10, 'compression', 'Cinepak')
Random coloring schemes with stripes can be created as follows:
coloring = rand(M,N+1)
c = rand(1,N+1);
coloring = repmat(c, [M 1]);
c = rand(M,1);
coloring = repmat(c, 1, N+1);
I just tried your code, the surface actually rotates while I run it in MATLAB, but when I save the file by movie2avi commend and try to open it later with the real player, windows media player, or even VLC, it doesn't work..It just shows the surface, no rotation...
ReplyDeleteHello,
ReplyDeleteThe code was tested using MATLAB 2008b on a Windows XP SP3 box. The movie generated played without a problem on Windows Media Player v11 and DivX 7 without a problem. So I assume that you are using Windows Vista or 7.
I know that some codecs are not supported any more for Vista so that creates some problems. Several people have reported similar issues at the MATLAB newsgroup but I am not certain that there is a fix available.
I have played around with a function called 'mpgwrite' available at
http://www.mathworks.com/matlabcentral/fileexchange/309
but it seemed a bit clunky.
Some people had success with uncompressed movies though files get quite large.
You might also want to see:
http://www.mathworks.com/support/tech-notes/1200/1204.html#Section 11
http://www.mathworks.com/support/solutions/en/data/1-4G50RI/