wonderingdev
wonderingdev

Reputation: 1181

Issue: Confusing: Index must be a positive integer or logical

I've got such a problem using MATLAB:

I wrote this function:

function E = f(x, lamda)

E = 1 - exp(-lamda * x);

end

When I write: Prob = f(1000, lamda); where lamda = 3.4274e-004 I get this error:

??? Attempted to access f(1000,0.000341565); index must be a positive integer or logical.

I understand that it requires a positive integer, but why ? I need lamda to be real. What's the problem here ? Can you, please, tell me where I'm wrong?

Upvotes: 0

Views: 122

Answers (2)

Tamás Szabó
Tamás Szabó

Reputation: 733

Your error message indicates that there is a variable called f in your workspace and matlab thinks you are trying to access its elements. Remove the variable f with clear('f') or rename the function to something else and you should be fine.

Upvotes: 1

rayryeng
rayryeng

Reputation: 104555

You have a function f and a variable f declared at the same time. Do clear f; then try your code again. What's happening here is that the variable declaration takes precedence over your function and so doing f would try and access the variable f first.

If you're using f as a variable somewhere and can't change this, then rename your function to be something other than f... perhaps... comp or something. Once you do this, make sure you change your file name so that it's called comp.m, then do:

Prob = comp(1000, lamda);

Upvotes: 2

Related Questions