Using a config file to specify model set-up (.json or .env??)

22 views (last 30 days)
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.

Accepted Answer

Steven Lord
Steven Lord on 6 May 2025
MATLAB has functions for reading in data from JSON files, see this documentation page for a list.
It also has functions for reading in many other file formats, see here.
  3 Comments
Steven Lord
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)
The speed of light in a vacuum is 299792458 m/s.
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
According to https://en.wikipedia.org/wiki/Gravitational_constant: The gravitational constant is 6.674300e-11 m^3/(kg*s^2). According to https://en.wikipedia.org/wiki/Speed_of_light: The speed of light in a vacuum is 299792458 m/s.
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
Jo
Jo on 7 May 2025
Hi Steven,
Thank you for your detailed response, I hadn't considered utilisaing a function but this would work quite nicely with our existing code base :) thank you for your assistance!

Sign in to comment.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!