Best way to initialize different parameters based on the machine in which I run the code

4 views (last 30 days)
Alberto Natali
Alberto Natali on 20 May 2022
Commented: Alberto Natali on 20 May 2022
Hi everyone,
I am currently in a stage where I would like to have my code modularized and following software-engineering techniques to make it reusable and understandable. In particular, I run my code either in my laptop and in an external server.
My goal is to have the main part of the code exactly the same in the laptop and the server, but different initialization parameters in the two case (in the server I will increase the # of iterations for instance and other variables). What is the common practice to do so, apart from clearly an if-else statement in the main?
I was thinking of an initialization file (like a JSON) in the laptop and the server, which is different and I just need to modify the values. Or a Matlab function which initializes the variables, but still, it would have an if-else statement.
Other suggestions? Keep in mind I might to want to extend the algorithmic part and introduce new parameters in the future.

Answers (1)

Jan on 20 May 2022
Edited: Jan on 20 May 2022
Matlab's setpref and getpref command is very useful also to store machine specific parameters.
A drawback is, that these settings are hidden in the User folder. Do not store "too" specific parameters there, but implement "important" settings in code, which is shared with all versions of your software. Example:
function Settings = MachineDependencies(doSetup)
if nargin < 1
doSetup = false;
if ispref('ANatali', 'Prefs')
Settings = getpref('ANatali', 'Prefs');
Settings.isServer = [];
if isempty(Pref.isServer) || doSetup
% Open a question dialog to clarify, if this is a "server"
Settings.isServer = givenAnswer;
setpref('ANatali', 'Prefs', Settings);
And inside the code:
function YourCalculations
Settings = MachineDependencies();
if Settings.isServer
iterations = 100;
iterations = 10;
Then all relevant decisions are visible inside the code. If you store the number of iterations inside the preferences, reading the code does not reveal all relevant details.
A drawback ist that setpref stores the settings for a specific user. If you want a dependency to the machine, you need another option, e.g. the JSON file inside a specific folder. If you implement this, it would be straighter to store machine and user dependencies at the same location. Take a look into the code of setpref and prefutils to write your own implementation. Then you can implement an additional logic to decide if the user or the machine setting has the higher precedence.
I've written such a tool to manage preferences on user, machine, lab and global level, e.g. to switch the language or error messages and for default values for the physical setup. "Global" level means e.g. the version number of the newest version taken from a web service, even if it is not installed locally. The actual preferences are as lean as possibel (logical flags, name of the language, etc.) and when I rollout a new version, the preferences files are not shipped.
  1 Comment
Alberto Natali
Alberto Natali on 20 May 2022
Thank you for your answer. Is this somehow recommendable?
I know that in other languages there is a config script which loads all the relevant path and e.g., YAML files, to initialize the parameters. In my case I would like to have something similar, so that in my main file I just run a setup.m file, which fetch the parameters stored in a text file which are in the current directory. This text file has different values for different machines. Is not this the optimal way?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!