Output argument "name of variable" (and possibly others) not assigned a value in the execution with "function name" function. Can someone explain what this means?

360 Ansichten (letzte 30 Tage)
I am calling a function from the command line but it does not save the output and return it like it is supposed to. rather it gives me this error "Output argument "name of variable" (and possibly others) not assigned a value in the execution with "function name" function. "
  1 Kommentar
Stephen23
Stephen23 am 12 Mai 2022
You did not define the output variable. For example, output C is not defined anywhere in the function:
out = myfun(pi)
Output argument "C" (and possibly others) not assigned a value in the execution with "solution>myfun" function.
function C = myfun(x)
A = sqrt(x);
B = acos(x);
end

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Steven Lord
Steven Lord am 12 Mai 2022
Another common cause of this error is that the function contains at least one code path on which the output variable is defined and at least one on which it is not. In the example below y is defined if x is greater than 0 or if x is less than 0. It's not defined if x is exactly 0.
result1 = example1717445(1)
result1 = 2
resultm1 = example1717445(-1)
resultm1 = 2
result0 = example1717445(0)
Output argument "y" (and possibly others) not assigned a value in the execution with "solution>example1717445" function.
function y = example1717445(x)
% y = NaN;
if x > 0
y = 2*x;
elseif x < 0
y = -2*x;
end
end
One way to detect or avoid this is to unconditionally assign a value to that output at the top of the function. Make a copy of the example code and uncomment the line that assigns NaN to y and that third call will no longer error, as there's no longer a code path that doesn't define y.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by