Using a config file to specify model set-up (.json or .env??)
22 views (last 30 days)
Show older comments
Hi all,
I am brand new to MATLAB and previously coded in Python. Previously, when using environmental models (such as a crop growth model) in Python, I would use a configuration file, which in this case was a .json file, to specify the model parameters (e.g. plant photosynthesis rate) that could then be read by the model to set it up. In this way I only changed the .json file for customising model runs for different locations and years. Now that I have changed jobs and I am working in MATLAB I am wondering the most appropriate way to do something similar. If I want to have a configuration file that contains all the information needed to customise a model run (e.g. the filepath for the save location, name for the save file, years to run the model for, method to use), what kind of file and structure for the model should I use?
I have been reading about .env files, which could be useful to help me set environmental variables which would be visible to multiple files within the same model, but then I wasn't sure if that is good practice. If I use a .json file and I want the information in it to be visible for all files in my model I believe I may have to use MATLAB preferences to do this - again I wasn't sure if this was good practice. These are my options as I understand them, but I would be very grateful for the advice of the community on this matter as I begin my MATLAB journey! Thankyou.
0 Comments
Accepted Answer
Steven Lord
on 6 May 2025
3 Comments
Steven Lord
on 7 May 2025
It depends on what works best for your usage.
If you already have the .json files that you want to use (that you previously used with your Python code, which BTW you may be able to call directly in MATLAB; see this section in the documentation) then using them in MATLAB is perfectly fine. Personally, as someone whose primary coding language is MATLAB, I might just write configuration functions that return a struct array, dictionary, table, etc. of parameters or a configuration class with properties.
data = initializeParameters();
fprintf("The speed of light in a vacuum is %d %s.\n", ...
data.c.value, data.c.units)
There's more advanced things I could do with this, like using dynamic field names to iterate over the parameters. But if you know specifically what data you want to access, as in the example above, you can just index into the data.
parameters = fieldnames(data);
for k = 1:numel(parameters)
P = data.(parameters{k}); % dynamic field names
fprintf("According to %s:\nThe %s is %d %s.\n\n", ...
P.source, P.description, P.value, P.units)
end
Here's the function containing the parameters. [Were this a real part of an application, I'd probably choose a more specific name than just initializeParameters, something like physicalConstants.m that more clearly indicates the purpose of the file.]
function data = initializeParameters
% initializeParameters Returns data about physical constants
%
% Each field in data is a struct array listing information about a physical
% constant. I could include information about what the fields in those
% structs are here in the documentation if I were planning on using this
% function as a part of a real application rather than an example on Answers.
data.g.description = "gravitational constant";
data.g.value = 6.6743e-11;
data.g.units = "m^3/(kg*s^2)";
data.g.source = "https://en.wikipedia.org/wiki/Gravitational_constant";
data.c.description = "speed of light in a vacuum";
data.c.value = 299792458;
data.c.units = "m/s";
data.c.source = "https://en.wikipedia.org/wiki/Speed_of_light";
end
More Answers (0)
See Also
Categories
Find more on JSON Format in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!