Having difficulty with a very function that returns multiple outputs (or should return multiple outputs)
18 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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?
0 Commenti
Risposta accettata
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
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
Più risposte (1)
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.
Vedere anche
Categorie
Scopri di più su Multirate Signal Processing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!