2

I've a problem with a function that I've written by myself. I've not found in any question how to solve it by myself so I post it here hoping that someone's going to see what I didn't see... (It's my first question and I don't know how to format it in Matlab code, I'm sorry about it)

Here is my function, which calculates the incident angle of a ray (given by the segment CD) on a 'wall' (given by AB). AB (the wall) is supposed to be horizontal or vertical and segment CD and wall AB are supposed to have an intersection :

function angle = incidentAngle(AB,CD)

%AB and CD are two vectors of size 1x4
%AB = [a1 a2 b1 b2] et CD = [c1 c2 d1 d2]
%AB is a segment from the point (a1,b1) to the point (a2,b2)


    if(AB(1)==AB(3)) % The wall is vertical
        if(CD(1)==CD(3)) % The "ray" is vertical too, the angle is 90° or pi/2
            angle = pi/2;
        else
            angle = abs(atan((CD(2)-CD(4))/(CD(1)-CD(3))));
        end
    else
        if(AB(2)==AB(4))% The wall is horizontal
            if(CD(2)==CD(4)) % The "ray" is horizontal too, the angle is 90° or pi/2
                angle = pi/2;
            else
                angle = abs(atan((CD(1)-CD(3))/(CD(2)-CD(4))));
            end
        end
    end

end

The problem (for example) is that :

when I define AB and CD as

AB = [0 1 0 5];
CD = [-1 2 3 4];

and that I do

angle = incidentAngle(AB,CD)

in the command window, I get the message error :

??? Subscript indices must either be real positive integers or logicals.

And I don't understand why...

If now I just copy the function in the command window, like this :

AB = [0 1 0 5];
CD = [-1 2 3 4];

angle = 0;

if(AB(1)==AB(3)) % The wall is vertical
    if(CD(1)==CD(3)) % The "ray" is vertical too, the angle is 90° or pi/2
        angle = pi/2;
    else
        angle = abs(atan((CD(2)-CD(4))/(CD(1)-CD(3))));
    end
else
    if(AB(2)==AB(4))% The wall is horizontal
        if(CD(2)==CD(4)) % The "ray" is horizontal too, the angle is 90° or pi/2
            angle = pi/2;
        else
            angle = abs(atan((CD(1)-CD(3))/(CD(2)-CD(4))));
        end
    end
end

angle

I get the right answer,

angle =

0.4636
mwoua
  • 403
  • 5
  • 13
  • I made a new function in my workspace and tried to re-create your error using your inputs and it worked fine for me. Not quite sure what the issue is. I'm using R2012b btw – anonymous Apr 25 '13 at 17:21
  • What I do not understand is why it works fine but not when I call the function... The function is (I think) well written and the function is saved in the right folder and though it just don't run... – mwoua Apr 25 '13 at 17:39
  • Also see [this question](http://stackoverflow.com/questions/20054047/subscript-indices-must-either-be-real-positive-integers-or-logicals-generic-sol) for [the generic solution to this problem](http://stackoverflow.com/a/20054048/983722). – Dennis Jaheruddin Nov 27 '13 at 15:40

1 Answers1

3

There's a built in Matlab function called angle, overwriting it with your variable name is inviting trouble. Try to change that variable name, say with the name angl instead. This should solve your problem.

bla
  • 25,846
  • 10
  • 70
  • 101
  • 1
    @mwoua did you change the name of your function as well? – Dan Apr 25 '13 at 17:39
  • it worked for me when I used `function angl = incidentAngle(AB,CD)` and change all `angle` variable occurrences to `angl` – bla Apr 25 '13 at 17:41
  • When I changed all my "angle" to "angl", it didn't worked... But when I changed the function name to "incidentAngl", it worked. Thanks ! – mwoua Apr 25 '13 at 17:43
  • Just a last thing, how do you highlight (is it a correct word? I'm Belgian and do not know the right word... Anyway) a word like "function angl = incidentAngle(AB,CD)" in your comment ? – mwoua Apr 25 '13 at 17:44
  • use the ` sign before and after the word you want to highlight. – bla Apr 25 '13 at 17:57
  • @natan the problem is there... Again. Don't you have another idea to fix this ? – mwoua Apr 26 '13 at 19:42
  • yesterday you said it worked. what happened? I copied your code, change all the `angle` variable to a different name, and it worked for me. (as I also wrote already). – bla Apr 26 '13 at 21:17
  • @natan The problem is... I didn't change anything ! It sometimes says `"??? Index exceeds matrix dimensions."` which is impossible since I verified that all my arguments are 1x4 (and they are) and sometimes `"??? Subscript indices must either be real positive integers or logicals."` The fact that it's sometimes one and sometimes the other is really bizarre. – mwoua Apr 27 '13 at 15:45
  • The problem seem now to be only `??? Index exceeds matrix dimensions.`. Although, in my code, I do this :
    `firstSegment size(firstSegment) wall = mur(n,:) size(mur(n,:)) incidentTransmissionAngl2 = incidentAngl(wall,firstSegment);` And my command window returns me >> `firstSegment = 0 4.8889 2.5000 3.5000 ans = 1 4 wall = 5 10 5 6 ans = 1 4 ??? Index exceeds matrix dimensions.`
    – mwoua Apr 27 '13 at 16:17
  • It now works... I really don't understand what is happening. Anyway. It works. Praise the lord – mwoua Apr 27 '13 at 16:38