Unknown error in my m.function
Show older comments
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
More Answers (0)
Categories
Find more on Programming 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!