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

조회 수: 3 (최근 30일)
Dominic Riccoboni
Dominic Riccoboni 2016년 8월 11일
편집: per isakson 2016년 8월 11일
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

채택된 답변

Pawel Ladosz
Pawel Ladosz 2016년 8월 11일
편집: Pawel Ladosz 2016년 8월 11일
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
Dominic Riccoboni
Dominic Riccoboni 2016년 8월 11일
편집: per isakson 2016년 8월 11일
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!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by