Effacer les filtres
Effacer les filtres

I'm having trouble loading a cell array within my for loop

3 vues (au cours des 30 derniers jours)
Dominic Riccoboni
Dominic Riccoboni le 11 Août 2016
Modifié(e) : per isakson le 11 Août 2016
I'm trying to load a cell array with one column of arrays and one column of single strings and whose entries are defined at each n value of a for loop. I defined the dimensions of the cell array then tried to assign the inputs of the nth iteration to the nth row and appropriate column. I cant imagine why when I run the code my cell array comes out with empty inputs!! Additionally when I try to plot the cell array(2nd loop) I get the error message "Index exceeds matrix dimensions." and I don't understand why this is happening.
for n = (1:length(tnew));
R = [cos(statesnew(n,4)) sin(statesnew(n,4));
-sin(statesnew(n,4)) cos(statesnew(n,4))];
phi = (0:pi/40:2*pi)';
Geometry = r*[cos(phi) sin(phi);
NaN NaN;
-1/1.75 + 1/25*cos(phi), 1/1.75 + 1/25*sin(phi)
NaN NaN;
(-1/2 + 1/8) + 1/25*cos(phi), 1/2 + 1/25*sin(phi);
NaN NaN;
-1/2 + 1/25*cos(phi), (1/2 - 1/8) + 1/25*sin(phi);];
%Creates a circle with three smaller circles placed in an
%equilateral tringle that will all rotate and translate when
%the other arrays/matrices manipulate it.
Rotation = Geometry*R'; %applying the rotation matrix to the geometry
Translation = ones(length(Geometry),1)*statesnew(n,2)*[1 0];
%Creates an array of same length as the geometry
%array that places the x value of the current timestep in the first row
%so that is can be added to the x coordinate of every point of the
%geometry
NetMotion = Translation + Rotation; %rotating and translating the ball
if abs(statesnew(n,1) + (statesnew(n,3)*(-r))) < 0.0044;
Color = 'R';
else
Color = 'B';
end %If point of contact is about zero then the red bowling ball
%change its color to blue
NetMotionArray = cell(n,2);
NetMotionArray{n,1} = NetMotion;
NetMotionArray{n,2} = Color;
%Setting up a cell array so that the 4 referenced dimensions can be
%hidden in a two dimensional array
end
for n = [1:length(tnew)]
figure(4)
plot((NetMotionArray{n,1}(:,1)), (NetMotionArray{n,1}(:,2)), NetMotionaArray{n,2} );
axis ([-1 10 -10 10]);
pause(.035);
end

Réponse acceptée

Pawel Ladosz
Pawel Ladosz le 11 Août 2016
Modifié(e) : Pawel Ladosz le 11 Août 2016
Hi Dominic, I think you overwrite your cell array NetMotionArray with every iteration loop with line:
NetMotionArray = cell(n,2);
What this line means is that for every iteration loop you initialize new NetMotionArray and replace the old one.This line should be moved from inside a for loop to top of your code and should be changed to:
NetMotionArray = cell(length(tnew),2);
Let me know if that solved your problems.
  1 commentaire
Dominic Riccoboni
Dominic Riccoboni le 11 Août 2016
Modifié(e) : per isakson le 11 Août 2016
Such a simple solution, and it fixed the issue. I'll remember this for the future. Also, the other error message was related to the first issue so they were both solved at once. Thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots 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!

Translated by