Unknown error in my m.function

1 view (last 30 days)
I have designed a m.function in a matlab scripts that calculates the angle between a vector you enter in it as an input and the horizontal line (0º).
The script is that:
function [theta]=angulodrecta(V)
% You put an input in a vector form (for example V=[...]
%And the function gives you the angle between that vector and the
%positive horizontal (0º)
%it works supposing that x1=y1=0
y2=V(2,2);
x2=V(1,2);
y1=V(2,1);
x1=V(1,1);
m=(y2-y1)/(x2-x1);
if y2==0
if x2>0
elangulo=atand(m);
theta=elangulo
elseif x2<0
elangulo=atand(m);
theta=180+elangulo
end
elseif x2==0
if y2>0
elangulo=atand(m);
theta=elangulo
elseif y2<0
elangulo=atand(m);
theta=180-elangulo
end
elseif x2<0&&y2>0
elangulo=atand(m);
theta=180+elangulo
elseif x2<0&&y2<0
elangulo=atand(m);
theta=180+elangulo
elseif x2>0&&y2<0
elangulo=atand(m);
theta=270-elangulo
end
end
The question here is that, when I try to execute the following code:
%------------------------BLOCK A------------------------
L1=[0 5;0 0];
L2=[0 5;0 5];
L3=[0 0;0 5];
L4=[0 -5;0 5];
L5=[0 -5;0 0];
L6=[0 -5;0 -5];
L7=[0 0;0 -5];
L8=[0 5;0 -5];
%-------------------------------------------------------
%------------------------BLOCK B------------------------
hold on
axis equal
grid
%-------------------------------------------------------
%------------------------BLOCK C------------------------
plot(L1(1,:),L1(2,:),'r');
plot(L2(1,:),L2(2,:),'r');
plot(L3(1,:),L3(2,:),'r');
plot(L4(1,:),L4(2,:),'r');
plot(L5(1,:),L5(2,:),'r');
plot(L6(1,:),L6(2,:),'r');
plot(L7(1,:),L7(2,:),'r');
plot(L8(1,:),L8(2,:),'r');
%-------------------------------------------------------
%------------------------BLOCK D------------------------
[th1]=angulodrecta(L1)
[th2]=angulodrecta(L2)
[th3=angulodrecta(L3)
[th4]=angulodrecta(L4)
[th5]=angulodrecta(L5)
[th6]=angulodrecta(L6)
[th7]=angulodrecta(L7)
[th8]=angulodrecta(L8)
%-------------------------------------------------------
Matlab only calculates correctly the first angle (th1) and then it gives me the following error as you can see
What am I doing wrong? what is going wrong?
That error only occurs when I tell matlab to calculate one or more anglee, I mean, if the code introduced in the command window I only put one ([th4]=angulodrecta(L4) for example, or [th7]=angulodrecta(L7) or whatever) it works fine. The problem occurs when I put in the command window two or more of them.

Accepted Answer

Stephen23
Stephen23 on 25 Oct 2020
Edited: Stephen23 on 25 Oct 2020
There are a few combinations of values for which your function does not define the output argument. If you call the function with those values, it will throw an error becase the output is undefined. For example, consider y2==0 and x2==0. It might be that you expect the code evaluation to "fall through" like it does in some languages, but in MATLAB only one block of an if-elseif-...-else block will be executed, so once y2==0 is matched then that block will be executed, and if x2==0 then you do not define theta. Thus the error.
Tip1: note that the MATLAB approach would use simpler vectorized code, e.g. based on modulo and other vectorized operations. While your approach works for individual values, you should learn how to write code that handles entire vectors/matrices/arrays of data:
Tip2: numbering variables like that is a sign that you are doing something wrong. The MATLAB approach is to use indexing, which is automatically expandable, generalizable, does not require copy-and-paste of code, and makes both looping and code vectorization easy.
  3 Comments
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras on 26 Oct 2020
Okay, I will check it out again
Great Thanks Cheburashka!!

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!