## Attempted to access a(2); index out of bounds because numel(a)=1

on 13 Sep 2013

### Andrei Bobrov (view profile)

if i try to make a string 111100011 into 1111, 000, 11

```    function [indi] = SeparatePattern (a)
k=1;
for i=1:length(a)
while strcmp(a(i),a(i+1))
if strcmp(a(i),'0')
indi(1,k)='0';
else
indi(1,k)='1';
end
end
while a(i)~=a(i+1)
k=k+1;
end
end```

why i keep getting an error

```Attempted to access a(2); index out of bounds because numel(a)=1?
```

## Products

No products are associated with this question.

### Andrei Bobrov (view profile)

on 13 Sep 2013
Edited by Andrei Bobrov

### Andrei Bobrov (view profile)

on 13 Sep 2013
```a = '111100011';
indi = regexp(a,'0+|1+','match')
```

### Walter Roberson (view profile)

on 13 Sep 2013

What exactly are you passing for "a" ? Are you invoking

```t = SeparatePattern(111100011)
```

? If you are then you are passing a single decimal number (roughly one billion) to the function instead of a string. To pass a string in you would need to invoke

```t = SeparatePattern('111100011')
```

Also take note that you loop "i" from 1 to the length of a. Consider the last iteration, when "i" has already become length(a), so a(i) is the final element in "a". Inside the "for" you have

```while strcmp(a(i),a(i+1))
```

but if a(i) is the last element of "a", then a(i+1) is going to be past the last element of "a" and you would get exactly the kind of error you saw.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi