Unknown error in my m.function
1 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/391853/image.png)
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.
0 个评论
采纳的回答
Stephen23
2020-10-25
编辑:Stephen23
2020-10-25
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 个评论
Stephen23
2020-10-26
编辑:Stephen23
2020-10-26
"However I still don't understand why it works one by one..."
It doesn't. When I try L2 I get the same error (undefined output).
The cause is easy to find: you did not define the output theta if both x2>0 and y2>0.
I have not given you a complete, exhaustive list of all of the cases for which your function does not define theta, but so far I have shown you two cases, at least one of which actually occurs in your L data matrices. You need to revise the logic of your function yourself (I cannot tell you how because I do not know what the algorithm is).
I strongly recommend that you write vectorized code (e.g. using mod, etc) and avoid these verbose and buggy ifs.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!