Effacer les filtres
Effacer les filtres

How do I pass a dynamic number of output variables to a function?

31 vues (au cours des 30 derniers jours)
André Bueno
André Bueno le 18 Juil 2024 à 12:23
Commenté : Stephen23 le 19 Juil 2024 à 3:05
Some functions change their behavior dependending on the number of output variables. For example, ndgrid will create N N-Dimensional arrays where N is the number of output variables. I need to pass a number of output variables that depends on other variable (whose value is not previously known). Is it possible to do that? What is the best way to do that?

Réponses (3)

Star Strider
Star Strider le 18 Juil 2024 à 12:29
You can define as many outputs to a function as you want. In the call to the function, you can selectt specific outputs using the tilde (~) to suppress the outputs you do not need.
You can also use the varargout function.

Steven Lord
Steven Lord le 18 Juil 2024 à 14:00
I need to pass a number of output variables that depends on other variable (whose value is not previously known).
I wouldn't use the word "pass" here. Nothing from the output variables enters the workspace of the function you're calling unless those variables are also specified as input arguments.
But to answer the question about how to specify output arguments when you don't know until run-time how many there will be, use a comma-separated list. See the Function Return Values section on that page for an example that calls fileparts with three output arguments. While that example hard-codes 3 output arguments, you could create that cell array C using size information computed using a variable.
S1 = callSVD(1) % 1 output
S1 = 4x1
34.0000 17.8885 4.4721 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[U3, S3, V3] = callSVD(3) % 3 outputs
U3 = 4x4
-0.5000 0.6708 0.5000 -0.2236 -0.5000 -0.2236 -0.5000 -0.6708 -0.5000 0.2236 -0.5000 0.6708 -0.5000 -0.6708 0.5000 0.2236
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S3 = 4x4
34.0000 0 0 0 0 17.8885 0 0 0 0 4.4721 0 0 0 0 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
V3 = 4x4
-0.5000 0.5000 0.6708 -0.2236 -0.5000 -0.5000 -0.2236 -0.6708 -0.5000 -0.5000 0.2236 0.6708 -0.5000 0.5000 -0.6708 0.2236
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
In fact, you could use nargout in callSVD and avoid having to pass in the number of outputs.
S1 = callSVD2 % 1 output
S1 = 4x1
34.0000 17.8885 4.4721 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[U3, S3, V3] = callSVD2 % 3 outputs
U3 = 4x4
-0.5000 0.6708 0.5000 -0.2236 -0.5000 -0.2236 -0.5000 -0.6708 -0.5000 0.2236 -0.5000 0.6708 -0.5000 -0.6708 0.5000 0.2236
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S3 = 4x4
34.0000 0 0 0 0 17.8885 0 0 0 0 4.4721 0 0 0 0 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
V3 = 4x4
-0.5000 0.5000 0.6708 -0.2236 -0.5000 -0.5000 -0.2236 -0.6708 -0.5000 -0.5000 0.2236 0.6708 -0.5000 0.5000 -0.6708 0.2236
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function varargout = callSVD(n)
varargout = cell(1, n);
[varargout{:}] = svd(magic(4));
end
function varargout = callSVD2 % No need for n here
varargout = cell(1, nargout); % Use nargout instead
[varargout{:}] = svd(magic(4));
end

Walter Roberson
Walter Roberson le 18 Juil 2024 à 17:51
OutputVariable = cell(1,NumberOfOutputsNeeded);
[OutputVariable{:}] = FunctionCall(parameters, as, appropriate);

Catégories

En savoir plus sur Operators and Elementary Operations dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by