function chaining when one function has multiple outputs

9 views (last 30 days)
Similar question has been asked before, but I would ask the question in a more specific manner. If the function in the input end has more than one output, there will be the error 'Not enough input arguments'. Is there a neat way to make chained function out of that situation?
f(g) % only the first output of g is kept as input for f
Not enough input arguments.

Error in solution>f (line 9)
out = x+y;
function [x, y] = g()
x = 1;
y = 2;
end
function out = f(x, y)
out = x+y;
end
Storing the outputs of the first function in two variables could be a solution, but I like to see how chaining could be done in this kind of scenario for the sake of learning.
[x, y] = g();
f(x, y)
ans = 3
  2 Comments
Stephen23
Stephen23 on 20 Aug 2024
Edited: Stephen23 on 20 Aug 2024
"Is there a neat way to make chained function out of that situation? "
No. The MATLAB apporoach is to call the two functions on two lines: this works because MATLAB's function outputs are demand driven. A discussion of why this behavior is required:
Simon
Simon on 20 Aug 2024
Thanks for the very clear explanation. It's worth of keeping the max(max) example in mind.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 19 Aug 2024
Suppose you had a function that could be called with either 1 output or more outputs, and a function that can accept 1 input or multiple inputs. If you chained those two so that the output(s) of the first of those functions were passed into the second function, how should MATLAB know with how many outputs to call the first function? To give an example (not necessarily a realistic example but just to illustrate the situation):
A = magic(4);
L = lu(A);
oneOutputCase = max(L)
oneOutputCase = 1x4
16.0000 13.5000 14.2500 13.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[L, U] = lu(A);
twoOutputCase = max(L, U)
twoOutputCase = 4x4
16.0000 2.0000 3.0000 13.0000 0.3125 13.5000 14.2500 0 0.5625 0.4352 1.0000 5.6667 0.2500 1.0000 0 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
chainCase = max(lu(A))
chainCase = 1x4
16.0000 13.5000 14.2500 13.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Were you expecting the twoOutputCase output from the chainCase call? The lu function can return up to five outputs (though the fourth and fifth are only supported for sparse inputs, but the first three are supported for both sparse and full inputs) and the max function can accept (as data inputs) one or two inputs. How would you expect MATLAB to distinguish between those two cases?
  1 Comment
Simon
Simon on 20 Aug 2024
I see what the interpreting machine behind Matlab might see from your example. A human mathematician has the leisure to 'boldly' use symbols like max(min) or min(max) because he has tacit context to rely on.

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!