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
Adam Danz
2019년 2월 14일
Here are two alternatives.
- "Varargin" is a viable solution (as you mentioned).
- Reducing the function to 1 input and using a cell array to pass multiple variables through the single input
Umair Ali Shah
2021년 4월 27일
I have same issue function cannot evaluate array and manual input is not a solution.
Jonas
2021년 4월 27일
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
답변 (1개)
As mentioned in the comments by Matt J and myself under this question, the only two recommended solutions to looping over input variables is
- pack your inputs into a single array variable
- use varargin which automatically packs all inputs into a cell array.
Then you can loop over all elements of the array.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!