Language reference
Multiple outputs
Declare ordered results, discard positions and bind built-in outputs.
EN / PT: One left-division output
EN: mldivide(A,B) has exactly two arguments and one MATRIX output X n-by-p, even 1-by-1. @mldivide works; extra outputs raise Sun:InvalidOutputCount. Binary A\B is one expression, not a multiple-output call. PT: mldivide(A,B) tem exatamente dois argumentos e uma saida MATRIZ X n por p, inclusive 1 por 1; aceita @mldivide, mas saidas extras geram Sun:InvalidOutputCount. See Linear algebra / Algebra linear for shape-only LU/SVD dispatch and limits.
Declare and request outputs
User functions declare zero to 32 unique named outputs. A regular expression or single assignment requests the first output; bracketed assignment requests ordered positions. Results remain ordinary Workspace values, not tuples.
[averages, deviations] = summarize([1, 2, 3; 4, 5, 6]);
function [averages, deviations] = summarize(values)
averages = mean(values, 2);
deviations = std(values, 0, 2);
endTargets and discards
The right side must be a direct named call, including a variable holding a named function handle. Each of 1 to 32 comma-separated target positions is a unique nonreserved identifier or ~. Discard consumes an output position without binding a variable; declaration outputs cannot use ~. Indexed/field targets, empty lists and multiple rows are not supported.
Only requested, nondiscarded outputs must be assigned by a user function. Too many requested outputs fail before entering its body. A no-output function cannot be used as a value, even through a discard. Variable output lists are not implemented.
data = [4, 9, 9];
[~, index] = max(data);
[rows, cols, third] = size(data);
[row, col, values] = find(data);EN: Requested outputs
nargout() is valid only inside a user-defined or anonymous function and returns the number of output positions requested by its caller: 0 for a statement call, 1 in an expression or single assignment, and the full target count in multiple assignment, including ~. It does not inspect a function's declaration; nargout(f) is unsupported.
isargout(k) returns a boolean: true only for a requested, nondiscarded position; false for ~ or any position beyond the requested count. k must be a positive finite real integer Number, not a boolean, complex value or 1-by-1 matrix. Both functions are interpreter-only, invalid at script level and forbidden in parfor. nargin and variable argument/output lists are unsupported.
Each nested call has its own count and discard mask. Returning or throwing from an inner call restores the enclosing call context. The example prints only true; discarded outputs need not be assigned.
inspect(0, false);
count = inspect(1, true);
disp(count == 1);
[count, ~, third] = inspect(3, true);
disp(count == 3 && third);
function [count, second, third] = inspect(expected, firstRequested)
disp(nargout() == expected);
disp(isargout(1) == firstRequested && !isargout(2) && !isargout(4));
if isargout(1)
count = nargout();
end
if isargout(3)
third = isargout(3);
end
endPT: Saidas solicitadas
nargout() so pode ser usada dentro de uma funcao de usuario ou anonima: retorna 0 em chamada como instrucao, 1 em expressao ou atribuicao simples e a quantidade total de posicoes em atribuicao multipla, inclusive ~. Nao consulta a declaracao: nargout(f) nao e suportada.
isargout(k) retorna booleano true somente para uma posicao solicitada e nao descartada; retorna false em ~ ou alem da contagem solicitada. k deve ser Number real inteiro positivo finito, nao booleano, complexo nem matriz 1 por 1. Ambas exigem interpretador, sao invalidas no nivel do script e proibidas em parfor. Nao ha nargin nem listas variaveis de argumentos ou saidas.
Chamadas internas usam contagem e mascara proprias; ao retornar ou lancar erro, restauram o contexto externo. Todos os resultados abaixo devem ser true; chamadas internas nao significam suporte a declaracoes de funcoes aninhadas.
[count, ~, restored] = outer();
disp(count == 3 && restored);
function [count, skipped, restored] = outer()
count = nargout();
requested = @() nargout();
active = @(position) isargout(position);
restored = requested() == 1 && active(1) && !active(2);
inspectInner();
try
failInner();
catch exception
restored = restored && nargout() == 3;
end
restored = restored && nargout() == count && isargout(1) && !isargout(2) && isargout(3) && !isargout(4);
end
function inspectInner()
disp(nargout() == 0 && !isargout(1));
end
function failInner()
error("Docs:Inner", "Restore caller context");
endBinding and errors
Arguments are evaluated once. Results are collected and validated before all named destinations are updated together. Evaluation or return-validation errors leave existing destinations unchanged and do not create new ones. Argument/body I/O effects are not rolled back.
Missing requested outputs raise Sun:UnassignedOutput; invalid output counts raise Sun:InvalidOutputCount. Without a semicolon, results print in target order after binding; an output-host failure while printing does not undo the assignment.
Built-in contracts
- size(A): one shape vector, or separate dimensions up to 32 outputs; dimensions after the second are 1. size(A, dim) is single-output.
- min/max reductions: values and optional first-occurrence indices. Pairwise forms are single-output.
- find: linear indices, or row/column coordinates, optionally followed by values preserving real/logical/complex type. Count and direction apply before outputs are formed.
- qr: one output is R; two give unpivoted [Q,R]; three positions activate pivoted [Q,R,P] or economy [Q,R,p], even when the third is ~. Only numeric 0 selects economy.
- svd: one output is singular values; two give [U,S]; three give [U,S,V]. Numeric 0 or 'econ' selects economy; ~ skips unrequested factors. See Linear algebra / Algebra linear for PT/EN shapes and examples.
- Other built-ins keep their existing one-output or effect-only contracts.
Projects and execution modes
The same syntax works in automatically loaded function files. Studio recognizes output lists in document symbols, function signatures and Workspace bindings; debugger stepping treats the assignment as one statement.
Multiple-target assignments are not supported in parfor, including pure built-ins. LLVM rejects them with interpreter guidance.
EN / PT: LU and Cholesky outputs
EN: lu(A) returns packed m-by-n, not U alone. [L,U] has shapes m-by-k/k-by-n and A=L*U with inverse row permutation absorbed into L; [L,U,P] has unit lower trapezoidal L and P m-by-m, P*A=L*U. chol(A[,triangle]) returns upper/lower factor; [factor,p] returns Number p=0 or first failed leading minor, with partial square factor (p-1)-by-(p-1). ~ skips discarded factors; status catches only non-positive-definiteness, not other errors. See Linear algebra for full contracts.
PT: lu(A) retorna matriz compactada, nao U; duas saidas absorvem permutacao inversa em L, tres satisfazem P*A=L*U. chol retorna fator e opcional status Number p; falha de positividade retorna fator parcial quadrado (p-1) por (p-1). ~ evita fatores descartados; outros erros continuam erros. Atribuicao multipla continua proibida em parfor e LLVM.