Creating discrete variables within a for loop
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Cedric
le 22 Août 2015
Modifié(e) : Cedric
le 22 Août 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 commentaire
Cedric
le 22 Août 2015
Modifié(e) : Cedric
le 22 Août 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/180684/image.png)
David Young
le 22 Août 2015
Modifié(e) : David Young
le 22 Août 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.
Voir également
Catégories
En savoir plus sur Graphics Performance dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!