3

I am not new to Matlab, but I have a very strange error I have not encountered before under the circumstances. I have tried to make a simplified version of my code to demonstrate the error I am getting. Basically, I have the indexing error, but the index being accessed is a positive integer.

I am wondering if it may have something to do with the format of the index number. Anyway, any help or suggestions would be greatly appreciated.

t=(0:0.00001:2*pi);
Cir(1,1:length(t)) = 0;
Cir(2,1:length(t)) = 0;
Cir(3,1:length(t)) = 0;

D0(1:length(t)) = 0;

% Search for the first minimum
[min1D0,t1Found] = min(abs(D0(1:length(t)/2)));
% Search for the second minimum
[min2D0,t2Found] = min(abs(D0(length(t)/2:length(t))));
t2Found = t2Found + length(t)./2; % Add tIndex/2 to correct the index

C1 = [Cir(1,t1Found),Cir(2,t1Found),Cir(3,t1Found)];
C2 = [Cir(1,t2Found),Cir(2,t2Found),Cir(3,t2Found)];

The output is:

Warning: Integer operands are required for colon operator when used as index 
Warning: Integer operands are required for colon operator when used as index 
??? Attempted to access Cir(1,314161); index must be a positive integer or logical.

Note: I am using R2009b.

4

1 に答える 1

2

length(t) is 628319, which is an odd number.

You are trying to access

DO(length(t)/2)

where length(t)/2 is 314159.5.

Array indices need to be integers, that is why you are getting that warning.

The same thing happens with t2Found. It is not an integer.

t2Found = t2Found + length(t)./2;

results in 314160.5.


As a side note, you can use the zeros function for preallocation.

Cir = zeros(3, length(t));

will give you the same result.

于 2013-01-02T07:22:15.340 に答える