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

2 views (last 30 days)
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

Accepted Answer

Pawel Ladosz
Pawel Ladosz on 11 Aug 2016
Edited: Pawel Ladosz on 11 Aug 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 Comment
Dominic Riccoboni
Dominic Riccoboni on 11 Aug 2016
Edited: per isakson on 11 Aug 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!

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming 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!