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

41 ビュー (過去 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 Center および File ExchangeSpectral Measurements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by