필터 지우기
필터 지우기

Best practice to pass large number of parameters to many functions?

조회 수: 110 (최근 30일)
Andras
Andras 2014년 8월 1일
댓글: Ajay 2022년 11월 10일
This a programming style question. I have to solve a model for given parameters. The parameters are fixed in the beginning and they do not change during execution. These parameters need to be available to all the functions and subfunctions of the code. So far I used global variables. This is not very elegant, but it worked. But when I tried to speed up my code with using "parfor" instead of "for", then I learned that globals and parfor don't play well together.
So now I am planning to pass all the parameters to all functions as input arguments. Obviously, I don't want to write out each of them and create functions with 20-30 inputs. What is the best practice to do this? Should I use a structure?
Thanks, Andras
  댓글 수: 6
Hari Haran
Hari Haran 2018년 10월 4일
편집: Hari Haran 2018년 10월 4일
Hi,I am new to matlab .When I tried to execute the program(I have attached 2 files),the output shows an error saying too many input arguements.Can anyone tell me what's the problem?
Adam
Adam 2018년 10월 5일
You would be better off opening a new thread to ask a question. I assume you are just pressing Run on your program though, in which case obviously it will not have any input arguments, which it appears to expect so you will get that error as soon as it reaches a line where one of those input arguments will be used.

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2014년 8월 1일
편집: John D'Errico 2014년 8월 1일
Yes. Simplest and fastest would be to use a struct as a container. That way you pass around only one argument to each function where any parameter is needed. This is not costly in terms of space or time, since MATLAB does not copy the struct into each workspace unless it is modified there.
Another option which will surely be slower but does not need you to pass in anything, is to use a function. Have the parameters defined in there as persistent variables, which you could easily set up so they are passed in initially.
Then when you want to know the value of a given parameter, query the value using the name of that parameter. Let the function do the work here. It would be slower, but requires no passed arguments at all.
You could even set up a list of prefs with setpref, which you could then query the values using getpref. Of course, this would be even slower, since I think prefs are stored on disk.
  댓글 수: 2
Madgalena Rodriguez
Madgalena Rodriguez 2022년 6월 1일
Could you give an example of your explanation, please! I am new in MatLab and this is the best response but I am not familiar with structures or cantainer yet.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Kelly Kearney
Kelly Kearney 2014년 8월 1일
I use structures for this sort of thing. I also usually set up functions so that they can handle input as either structure or parameter/value pairs. When appropriate, the functions will also be coded to fall back on specific default values, so I don't always have to provide all the parameters.
  댓글 수: 4
Andras
Andras 2014년 8월 1일
Thanks again. The example function was very useful!
Ajay
Ajay 2022년 11월 10일
hi can u share the example model domain has expired

댓글을 달려면 로그인하십시오.


Adam
Adam 2014년 8월 1일
Nowadays I generally use classes with a regular setup of a class (or many collaborating, but one front end at least) to run the actual algorithm(s) or solve the model or whatever it is and an arguments/parameters class that I pass in.
The parameters class is always very simple, effectively just a struct, but with the added bonus that I use validateattributes in the set functions of all the parameters to ensure they are in the correct format before they get anywhere near the algorithm.
Using classes with parallel programming does require a bit of care though as I have made many mistakes with it!!

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by