Filter löschen
Filter löschen

subscript indices must either be real positive integers or logicals when using a function in a nested for?

2 Ansichten (letzte 30 Tage)
I use this function in a nested for and I get this error: subscript indices must either be real positive integers or logicals
Zpred = h(Xpred)
where Xpred is a matrix
function zk=h(x)
zk(1,:)=sqrt(900+(x(1,:)-30).^2);
zk(2,:)=x(2,:).*(x(1,:)-30)./zk(1,:)
can anyone help me to find the error

Akzeptierte Antwort

Image Analyst
Image Analyst am 28 Mai 2015
When you're calling this:
Zpred = h(Xpred)
at that point, it thinks h is an array, not a function like you thought.
Your script/function is not called h.m is it? Or else somethere else earlier in your code, you must have used h in a way that created an array called h.

Weitere Antworten (1)

Joseph Cheng
Joseph Cheng am 28 Mai 2015
Bearbeitet: Joseph Cheng am 28 Mai 2015
so when i run the code you have:
function zk=h(x)
zk(1,:)=sqrt(900+(x(1,:)-30).^2);
zk(2,:)=x(2,:).*(x(1,:)-30)./zk(1,:);
with the input
h(randi(10,2,1))
i don't get the error. Now with this type of error the fastest thing is for you to use the debugger and solve it. Put a breakpoint at the call, verify the input Xpred is actually what you want, there is no variable or other item already called h, and if everything checks out step inside the function where is the line that the error says it is?
  3 Kommentare
Joseph Cheng
Joseph Cheng am 28 Mai 2015
Bearbeitet: Joseph Cheng am 28 Mai 2015
you should not use 'h' to describe a function and a variable. Matlab is not smart enough to understand which one you're trying to use.
Here is an example: if my dog is called Brian and I have a friend called Brian, and I ask you to take Brian outside. You wouldn't not know which one to take out side would you? Call your function another name or use another variable name for h.
*sorry to anyone named Brian. It was the first name i could think of.
sH
sH am 28 Mai 2015
Bearbeitet: sH am 28 Mai 2015
no I meant that I'm calling function h for the second time. when I use it separately, I don't get any error but when I use it in a nested for in a larger simulation I get this error

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by