How to loop through all the input of a function

I have a simulink matlab function which has many inputs.
output = fcn(input1, input2, ... inputx )
Is there a way to iterate through every input on by one inside the function?
I know if the function take input as varargin I can use varargin{i} to loop through. But what if the input to the function is fixed?

4 commentaires

Matt J
Matt J le 14 Fév 2019
Modifié(e) : Matt J le 14 Fév 2019
Normally, if the inputs are something you would want to loop over, they shouldn't even be separate arguments in the first place. They should be combined into an array, and passed as a single argument.
Adam Danz
Adam Danz le 14 Fév 2019
This sounds like a potential can of worms.
Here are two alternatives.
  1. "Varargin" is a viable solution (as you mentioned).
  2. Reducing the function to 1 input and using a cell array to pass multiple variables through the single input
I have same issue function cannot evaluate array and manual input is not a solution.
Jonas
Jonas le 27 Avr 2021
aaand we could use the who() function at the start of the function together with the eval() function to work on the variables. but i would not suggest that

Connectez-vous pour commenter.

Réponses (1)

Adam Danz
Adam Danz le 27 Avr 2021
Modifié(e) : Adam Danz le 27 Avr 2021

0 votes

As mentioned in the comments by Matt J and myself under this question, the only two recommended solutions to looping over input variables is
  1. pack your inputs into a single array variable
  2. use varargin which automatically packs all inputs into a cell array.
Then you can loop over all elements of the array.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2017b

Question posée :

QY
le 14 Fév 2019

Modifié(e) :

le 27 Avr 2021

Community Treasure Hunt

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

Start Hunting!

Translated by