How can I save several axis using makehgtform?

Hello, I have a program that rotates a thin plate around one axis using makehgtform. After rotating the object I use getframe() to obtain the frames from the object rotating. I want to simulate for example 10 random axes and save all the frames in a variable that I will use to do other things later, how can I do that?
I want to change the values vx, vy and vz with random values between 0 and 1 to obtain 10 random axes, because right now I can only obtain one axis each time I run the code.
clc
clear all
%% THIN PLATE
vertex_matrix = [-1 0 -1; 1 0 -1; 1 0 1; -1 0 1];
faces_matrix = [1 2 3 4];
%% SIMULATE PLATE
hgtc = hgtransform('Parent',gca);
patch('Vertices',vertex_matrix,'Faces',faces_matrix,...
'FaceVertexCData',hsv(6),'FaceColor','White','FaceNormalsMode','auto','BackFaceLighting','reverselit','Parent',hgtc)
axis([-4, 4, -4, 4, -4, 4])
axis square
set(gca,'color','k') %Plot black background 'k'
az = 0;
el = 0;
view(az, el);
xlabel('x')
ylabel('y')
zlabel('z')
%% LIGHT
light('Position',[0 -5 0],'Style','local','Style','infinite'); %Place the light at infinity. The position specify the
%direction from which the light shines in parallel rays.
%% ROTATION PLATE
beta = 0.003067961576; %2*pi/2048 ---> 1 lap in 2048s
t = 0:1:2047;
for i=1:length(t)
vx=1;
vy=1;
vz=1;
RR = makehgtform('axisrotate',[vx vy vz],i*beta);
set(hgtc,'Matrix',RR);
% hp = impixelinfo;
% set(hp,'Position',[5 1 300 20]);
F(i) = getframe(gcf,[120 60 320 300]) ;
drawnow;
end

1 Comment

After trying different things I got this code:
Is there a better way to generate these random axes instead of using the for loops?
JN=2; %JN - 1 = Number of axes you want to create (example: if JN = 3 ---> 2 random axes)
x = rand(1,JN-1);
y = rand(1,JN-1);
z = rand(1,JN-1);
axisvector = cell(JN-1, 1);
for i=1:JN-1
v = [x(i) y(i) z(i)];
axisvector{i} = v;
end
Faxis = cell(JN-1, 1);
for j=1:JN-1
for i=1:length(t)
RR = makehgtform('axisrotate',[axisvector{j}(1,1) axisvector{j}(1,2) axisvector{j}(1,3)],i*beta);
set(hgtc,'Matrix',RR);
% hp = impixelinfo;
% set(hp,'Position',[5 1 300 20]);
F(i) = getframe(gcf,[120 60 320 300]) ;
Faxis{j} = F;
drawnow;
end
end
% f=figure;
% set(f,'Visible', 'off');%'animating in background
meanGrayLevelsNEW = cell(JN-1, 1);
for j=1:JN-1
for i=1:length(F)
% convert the image to a frame
grayFrame = rgb2gray(Faxis{j}(i).cdata);
% grayFrame = rgb2gray(F(i).cdata);
% Calculate the mean gray level.
meanGrayLevels(i) = mean(grayFrame(:));
meanGrayLevelsNEW{j} = meanGrayLevels;
end
end

Sign in to comment.

Answers (0)

Products

Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!