Get a function to return a comma-separated variable list?

19 vues (au cours des 30 derniers jours)
Mark Thomson
Mark Thomson le 25 Avr 2021
Commenté : Mark Thomson le 25 Avr 2021
Dear All,
for compactness, I was looking for a way to get a function, called with no output arguments, to return all the outputs (not just the first), analogous to e.g. C{:} for a cell array. The aim is to streamline certain codes, where the function will return a variable-length list of outputs to be used as a list of inputs for another function. As an example:
function test_output_vars()
figure(1), clf
list=plot_list1;
plot(list{:}) % Two-liner with intermediate cell array
hold on
plot(plot_list2) % Desired one-liner, but only captures first output variable
[x,y,lt]=plot_list2(); % Works of course, but not what I'm aiming at
end
function list=plot_list1()
list={1:10,1:2:19,'o-'};
end
function varargout=plot_list2()
varargout={1:10,1:2:19,'o-'};
nargout % indeed, will =1 when called with no outputs...
end
Is there any "simple" way to achieve this (besides some tricks with a custom class and overloading numArgumentsFromSubscript)?
Regards, MT

Réponse acceptée

Walter Roberson
Walter Roberson le 25 Avr 2021
Modifié(e) : Walter Roberson le 25 Avr 2021
No, this is not possible in MATLAB. Comma list expansion is syntactic, requiring a literal {} or expression followed by dot followed by field name.
  2 commentaires
Walter Roberson
Walter Roberson le 25 Avr 2021
plot(plot_list1().varargout)
function varargout = plot_list1()
list={1:10,1:2:19,'o-'};
if nargout > 0
varargout = list;
else
varargout = {struct('varargout', list)} ;
end
end
Mark Thomson
Mark Thomson le 25 Avr 2021
Dear Walter,
many thanks for the guidance. Okay, returning a structure array is at least a working option.
Regards, MT

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by