Index expression out of bounds. Attempted to access element 2. The valid range is 1-1.“2”

I keep getting this error, my desired output is to get abs(I(2,ii(a))-I(2,ii(a)-1) so that when ii(a)=2, I want I(2,2) minus I(2,1) which is 5.1 minus 5.0 : Index expression out of bounds. Attempted to access element 2. The valid range is 1-1."2".
The error is due to this line:
arr(1,ii(a)) = arr(1,ii(a)) + abs(I(2,ii(a))-I(2,ii(a)-1));.
I define I as the following in .mat file:
save data -v7.3 'I' load('data.mat','I');
I =
Columns 1 through 6
0 1.0000 2.0000 3.0000 4.0000 5.0000
5.0000 5.1000 5.2000 5.2000 5.5000 5.9000
Column 7
6.0000
6.0000

Answers (1)

Check the sizes of ii and I, check also if all values in ii are positive integer

8 Comments

All ii vlues are positive. The size of ii is 1x5, the size of I is 2x5. I is a timeseries object in .mat file, the first row is time and the second row is data. I want to access the data in row 2 instead of the time in row 1. Am I doing something wrong?
i read this link http://www.mathworks.co.uk/matlabcentral/answers/9866 answered by Arnaud Miege , is it not possible to access the data in row 2 by coding I(2,xx)??
I asked if all values in ii are positive and integer
Yes all ii vlues are positive and integer, Sir
function arr= fcn(I,count,oldval)
persistent integ_signal
if isempty( integ_signal)
integ_signal=zeros(1,5)
end
persistent a
if isempty(a)
a=zeros(1)
end
%integ_signal=zeros(5,1);
arr=zeros(1,5);
ii=zeros(1,5);
aa=zeros(2,5);
integ_signal=oldval;
coder.extrinsic('load');
aa=load('data.mat','I');
%for ii = 1:length(count)
%if count(ii) == 1
if count==1
a=1;
ii(a)=count;
integ_signal(1,ii(a)) = 10; % Initial Condition
end
if count ~= 1
a=count;
ii(a)=count;
integ_signal(1,ii(a))= integ_signal(1, ii(a)-1); % Or, (count(ii-1),1)
arr(1,ii(a)) = integ_signal(1,ii(a));
arr(1,ii(a)) = arr(1,ii(a)) + abs(aa(2,ii(a))-aa(2,ii(a)-1));
end
a = a + 1;
end
there are some problems in your code
function arr= fcn(I,count,oldval) % I is not used in your code
aa=zeros(2,5); this line is unnecessary, because just after you wrote also aa=load('data.mat','I'); in this case the result is a structure array named aa with field I , note that I has nothing to do with I in function argument. to get the value
aa=aa.I
Got it, i changed according to your suggestion and it worked, however the system is giving wrong output mathematically. Do you mind to have a look on this new post? I created new question http://www.mathworks.com/matlabcentral/answers/105388-calculation-error-system-always-gives-output-0-0-0-value-in-index-3-value-in-index-1-0

Sign in to comment.

Tags

Asked:

on 8 Nov 2013

Commented:

on 8 Nov 2013

Community Treasure Hunt

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

Start Hunting!