Borrar filtros
Borrar filtros

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

16 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

John R
John R el 2 de Nov. de 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 comentario
Miguel
Miguel el 16 de Dic. de 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

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 2 de Nov. de 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 comentario
Bolin
Bolin el 2 de Nov. de 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!

Iniciar sesión para comentar.

Categorías

Más información sobre Spectral Measurements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by