Index in position 1 is invalid. Array indices must be positive integers or logical values.
13 views (last 30 days)
Show older comments
I've attached my code below. I keep getting the issue of incorrect indexing but I'm not sure why this is as I have defined my for loop starting at 1. If someone could take a quick look and help me that would be great. I feel like I'm just overlooking something. The error occurs in line 36
4 Comments
Rik
on 6 Oct 2018
The same goes for using i as a loop counter. If by some strange reason it gets interpreted as the imaginary unit, you'll get these kinds of errors.
Accepted Answer
jonas
on 6 Oct 2018
Edited: jonas
on 6 Oct 2018
Here is something fishy.
sigma = func2(t,x,sigma_x,func1(kc,t))
...
function sigma = func2(t,M,sigma_M,func1)
If you want to pass the output of func2 as an input to func1 that's ok, but do not name the output func1. This confusion causes func1 to be interpreted as a variable, which is why you get this error. You can pass the function as an argument like you do, but you need to name the variable used in func2 something other than func1.
What you do is similar to writing:
func1 = func1(arg1,arg2)
func1 is now a variable, so the next time you call it it will be interpreted as such.
2 Comments
jonas
on 6 Oct 2018
Edited: jonas
on 6 Oct 2018
Well no wonder, since you changed both the variable name and the function call to func11... don't use obfuscating variable names if you want to write clear easy-to-debug code. If you still do not see it..
total = total + ((func11(temp1,t(n)) - func11(temp2,t(n)))/2)^2;
↑ ↑
remove these 1's, and you will be able to run the code. You are trying to use the function here right, and not a variable?
I don't even know why you pass the func11 variable to the func2 function, it seems you are not even using it.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!