MATLAB: Readability of matlab code

global variablesloadMATLABreadability

Hi,
I am coding using matlab and have many functions, some of which have more than a dozen input arguments. The majority of the input arguments of my different functions are variables that I read from an external file and that remain unchanged all throughout the calculations. Basically I read the external file once in my main script then give what has been read as input arguments to the other functions. For the readability of my code, I would like to not have that many input arguments and was wondering what is the best solution that would not negatively impact the performance of my code. I thought of the following:
  • use global variables, however I've read that it is best to avoid them
  • read the external file and save the different variables as .mat then load the arguments inside each function
  • define a cell variable in which I store all my arguments, e.g. cell={a;b;c} and give that as an input to my functions, then inside the function declare the variables as a=cell{1} etc
If anyone could help me choose the best solution or give me more ideas I would greatly appreciate it!

Best Answer

I recently had a similar question, and one of the options that was suggested was to use structures, e.g.
Function1_Parameter.Variable1 = 1
Function1_Parameter.Variable2 = 2
Function1_Parameter.Variable3 = 3
So then you only need to have the name of the structure (Function1_Parameter) in the input arguments of a function, and you can index whichever value you need, similar to your last suggestion.