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

18 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

John R
John R il 2 Nov 2011
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 Commento
Miguel
Miguel il 16 Dic 2018
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

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 2 Nov 2011
"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 Commento
Bolin
Bolin il 2 Nov 2011
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!

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by