Creating discrete variables within a for loop

조회 수: 3 (최근 30일)
Nicholas P.
Nicholas P. 2015년 8월 22일
편집: Cedric 2015년 8월 22일
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?

답변 (2개)

Cedric
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
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
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.
  댓글 수: 1
Nicholas P.
Nicholas P. 2015년 8월 22일
I've been trying cell arrays to store the shape coordinates, and it does work well, but the problem arises when I want to use set() to update data in the plot instead of replotting the shapes every time I change the coordinates.
Usually if I had something like:
X = [0 1 1 0];
Y = [0 0 1 1];
figure
H = fill(X,Y,'r');
And i wanted to change the coordinates and replot the shape, I would do:
set(H,'YData',Y+1)
set(H,'XData',X+1)
drawnow
Instead of using the fill() command again. Eventually I'm going to be moving dozens of shapes, creating new ones and destroying old ones, which will get very computationally intensive if I am constantly using fill() instead of set() and drawnow.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by