Creating discrete variables within a for loop
조회 수: 3 (최근 30일)
이전 댓글 표시
I'm trying to do something like the following:
for i = 1:n
H(i) = [0 1 1 0 ; 0 0 1 1];
end
Essentially I'm trying to create multiple shapes in a for loop, each shape with it's own distinct handle. This imaginary code above would create n squares with handles H1, H2, ... all the way to Hn. I know that this syntax is not correct, but I'm wondering if there is any function in matlab that behaves this way? I want to be able to alter the coordinates of each shape individually, but also move them as a group, something like:
for j = 1:n
H(j) = H(j) + 1
end
This would move all of the shapes by 1 in the x direction and 1 in the y. Again, this is not the right syntax, but you get the idea. Anyone know of a way to create discrete shapes and alter them within a for loop?
댓글 수: 0
답변 (2개)
Cedric
2015년 8월 22일
편집: Cedric
2015년 8월 22일
UPDATED
Here is a fun (maybe) example:
nShapes = 10 ;
nFrames = 100 ;
% - Build figure and axes.
figure() ;
set( gcf, 'Units', 'normalized', 'Position', [0.1, 0.1, 0.6, 0.8] ) ;
set( gca, 'XLim', [-15, 15], 'YLim', [-15, 15] ) ;
grid on ;
% - Add shapes.
H = cell( nShapes, 1 ) ;
for shapeId = 1 : nShapes
P0 = 20 * rand( 1, 2 ) - 10 ;
H{shapeId} = patch( P0(1)+[0, 1, 1, 0], P0(2)+[0, 0, 1, 1], 'b' ) ;
end
% - Define rotation matrix.
theta = -2*pi / nFrames ;
R = [cos(theta), -sin(theta); sin(theta), cos(theta)] ;
% - Move.
for frameId = 1 : nFrames
for shapeId = 1 : nShapes
H{shapeId}.Vertices = H{shapeId}.Vertices * R ;
end
pause( 0.1 ) ;
end
댓글 수: 1
Cedric
2015년 8월 22일
편집: Cedric
2015년 8월 22일
Now here is an updated version for Star Strider ;-)
nShapes = 40 ;
nFrames = 100 ;
% - Build figure and axes.
figure() ;
set( gcf, 'Units', 'normalized', 'Position', [1.1,0.1,0.6,0.8] ) ;
set( gca, 'XLim', [-12,12], 'YLim', [-12,12] ) ;
grid on ;
% - Define the pacman function.
pacman = @(x, y, varargin) patch( bsxfun( @plus, x(:)', 0.5*[0,cos(-3*pi/4:0.1:3*pi/4),0]' ), bsxfun( @plus, y(:)', 0.5*[0,sin(-3*pi/4:0.1:3*pi/4),0]' ), varargin{:} ) ;
% - Add pacmans.
P0 = 20 * rand( nShapes, 2 ) - 10 ;
h = pacman( P0(:,1), P0(:,2), [1,0.8,0.3], 'EdgeColor', [0,0,0] ) ;
% - Define rotation matrix.
dTheta = -4*pi / nFrames ;
R = [cos(dTheta), -sin(dTheta); sin(dTheta), cos(dTheta)] ;
% - Animate.
for frameId = 1 : nFrames
% Update vertices.
h.Vertices = h.Vertices * R ;
% Zoom in/out.
ofs = 6 * ( sin( frameId*dTheta )) ;
set( gca, 'XLim', [-12+ofs,12-ofs], 'YLim', [-12+ofs,12-ofs] ) ;
pause( 0.07 ) ;
end
David Young
2015년 8월 22일
편집: David Young
2015년 8월 22일
I'm not quite sure what you want to do, but it feels like cell arrays might be part of the solution. You can store arrays in them and then operate on the arrays:
n = 10;
for ii = 1:n
H{ii} = randi(9, 1, 4); % random array
end
disp('Original:')
for ii = 1:n
disp(H{ii});
end
for jj = 1:n
H{jj} = H{jj} + 1;
end
disp('Modified:');
for ii = 1:n
disp(H{ii});
end
Note that some authorities feel that i and j are bad choices of loop variable because they are also used for the imaginary unit in MATLAB.
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!