Clear Filters
Clear Filters

Function runs with no inputs and returns a logical 1

2 views (last 30 days)
I have written a function that is supposed to take in 4 complex numbers and run some calculations and return 1 complex number and 2 integers as the function output. However, when I attempt to call the function with 3 outputs in a script I get an error telling me the function is not giving me enough output values. In addition, I can run my function with no input values and get a logical 1 as an output. Does anyone know what is causing this?
Here is my code:
function [mtype, g, grashyorn] = grashof(W11, W21, Z11, Z21)
lw1 = abs(W11);
lw2 = abs(W21);
g = W11+Z11-Z21-W11;
lg = abs(g);
C = W21 - W11;
lc = abs(C);
%find the lengths of all the links
lengths = [lw1, lw2, lg, lc];
l = max(lengths);
s = min(lengths);
p = 0;
q = 0;
for i = 1:4
if lengths(i) ~= l && lengths(i) ~= s
if lengths (i) > q
q = p;
p = lengths(i);
end
end
end
%find l s p and q
mtype = 0;
if (l+s) < (p+q) || (l+s) == (p+q)
grashyorn = 1;
if lw1 == s || lg == s
mtype = 1;
else
mtype = 2;
end
else
grashyorn = 0;
end
  1 Comment
dpb
dpb on 26 Mar 2017
Doesn't look like the function you're actually running is the code you posted. This can happen if you make changes, don't have the latest version first in the search path or the like.
Use
which grashof
to see which is actually being called now,
clear grashof
to unload copy cached in memory and try again.
Certainly
>> grashof
Error using grashof (line 3)
Not enough input arguments.
>>
with this code fails as expected with no inputs...

Sign in to comment.

Answers (1)

Anoop Somashekar
Anoop Somashekar on 29 Mar 2017
If your function is returning multiple values then you need to store the return value of the function in a vector as shown:
[a,b,c] = grashof(1+2i, 2+3i, 4+2i, 5-11i)
I presume you are storing the return value in a scalar which will always capture the first element of the vector i.e. in your case it's returning the value of mtype. You can also verify this by interchanging the vector order in the function definition as shown:
function [g, mtype, grashyorn] = grashof(W11, W21, Z11, Z21)
In the above case, when you call the function from the command line it will return variable g.
As commented in the thread, if you have a variable defined with the same name as the function then when you try to call the function with no inputs, it returns the variable value rather than function return value. As suggested in the thread, try to clear grashof and rerun the function call.

Community Treasure Hunt

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

Start Hunting!