Creating discrete variables within a for loop

3 views (last 30 days)
Nicholas P.
Nicholas P. on 22 Aug 2015
Edited: Cedric on 22 Aug 2015
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?

Answers (2)

Cedric
Cedric on 22 Aug 2015
Edited: Cedric on 22 Aug 2015
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 Comment
Cedric
Cedric on 22 Aug 2015
Edited: Cedric on 22 Aug 2015
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

Sign in to comment.


David Young
David Young on 22 Aug 2015
Edited: David Young on 22 Aug 2015
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 Comment
Nicholas P.
Nicholas P. on 22 Aug 2015
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.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!