Borrar filtros
Borrar filtros

How to get the number of output arguments for a MATLAB expression?

5 visualizaciones (últimos 30 días)
Hello folks, I have to evaluate multiple expressions in loop. Suppose x and y are one dimensional vectors and below three operations are supposed to be performed.
1) min(x)
2) min(x) + min(y)
3) min(x+min(y))
I am using eval for evaluating these expressions.
for count = 1:3
[output,index] = eval(fun(count));
end
In case 1 and 3, I need the value of expression and index as eval command sends both. But in case 2, I wont get index with eval and it throws error.
[a,b] = min(x) + min(y)
Error using +
Too many output arguments.
So in order to generalize, is there any possible way to know in advance that the expression is going to send one or two output arguments?

Respuesta aceptada

Stephen23
Stephen23 el 30 de Mayo de 2017
Editada: Stephen23 el 30 de Mayo de 2017
"So in order to generalize, is there any possible way to know in advance that the expression is going to send one or two output arguments?"
How would you then call those functions with one, and those with two outputs? It would be possible by allocating a cell array and using a comma-separated list, or by using some incomprehensible eval-based code, but really either of those is going to ugly spaghetti code.
A much simpler solution would be to use a try and catch:
C = {@(x,y)min(x), @(x,y)min(x)+min(y), @(x,y) min(x+min(y))};
x = 4;
y = 3;
for k = 1:numel(C)
try
[out,idx] = C{k}(x,y)
catch
out = C{k}(x,y)
end
end
which works without error, and does not use ugly eval. Or you with Mfiles could use nargout, e.g.:
for k = 1:numel(C)
n = nargout(C{k});
out = cell(1,n);
[out{:}] = C{k}(x,y)
end

Más respuestas (1)

Walter Roberson
Walter Roberson el 30 de Mayo de 2017
The syntax you used for indexing and eval can only work with strings objects or symbolic expressions, but you cannot create a symbolic min() for deferred execution, so you must be using string objects.
In order to have a chance of determining the number of potential outputs, you need to parse the string.
The rule is this:
  • An expression can only ever potentially return multiple outputs if it written in the form of a function call
  • If the expression has more than one part joined by binary operator symbols, then the expression can only return a single output.
  • Of all of the MATLAB operators defined at https://www.mathworks.com/help/matlab/operators-and-elementary-operations.html, only the Set operators can potentially return multiple outputs
For example, 5 + min(x) can only return a single output, and you can determine that immediately by seeing that it is not in the form of a single function call. If you rewrite it as plus(5, min(x)) then you have to examine the function to see which one it is.
Once you have parsed the string enough to determine that it is in the form of a single function call, you can pull out the function name and call nargout() with it. If the output is 0 then it never returns anything; if it is 1 then it only ever returns a single output; if it is positive 2 or more then it expects multiple outputs; if it is -1 or more negative then it can potentially return multiple outputs; for example nargout('union') is -1

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by