Having difficulty with a very function that returns multiple outputs (or should return multiple outputs)

18 次查看(过去 30 天)
Hi guys, I have written an extremely simple function that calculates the absolute value of a complex number (modulus) and its angle in the complex plane.
The code is as follow:
function [r, theta] = c2a(complex)
%this function coverts a complex number to its corresponding angle
%notation
theta = atan(imag(complex)/real(complex))/pi*180;
r = abs(complex);
end
%end of function
This function is supposed to output two values but it only out one which is the abs(complex), it does not print out theta for some reason.
For example: if I write,
c2a(5+5i)
ans =
%it will only return the absolute value but not the angle
7.0711
I am not very experienced with functions, I usually just resolve this situation with a script but in any case, does anyone know why the function is not returning two values like I specified it to?

采纳的回答

John R
John R 2011-11-2
The problem is that you need to do this
[a b]=c2a(5+5i);
Matlab only returns the first output if you don't try and make it define multiple outputs.
For instance, if you said, "a=c2a(5+5i);", it would set a equal to "r", since that is the first output defined in your function.
If you wanted to suppress the output of r and only get theta you could say [~, a]=c2a(5+5i); Although the syntax of the "~" changes in some Matlab versions.
  1 个评论
Miguel
Miguel 2018-12-16
I used this in my simple function:
function [output_001] = flow(velocity_outlet,radius_outlet)
% Compute
flow = velocity_outlet.*(radius_outlet).^2.*pi;
ratio = 13.97e-3./flow;
% Results
output_001 = [flow, ratio] ;
end

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2011-11-2
"complex" is the name of a function call in MATLAB (that creates a complex number.) It is possible that it is treating the references to complex as calls to complex([]), creating empty complex numbers, and returning an empty value for theta. Maybe.
By the way, shouldn't you be using atan2() instead of atan() ? Otherwise you are only going to return angles in one half of the plane.
  1 个评论
Bolin
Bolin 2011-11-2
I didn't know that function existed in MATLAB, I've gotten to used to my TI 84 for these types of calculations. In fact I was just going to extend my function to include scenarios which includes the entire plane! Thank you for your help!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by