Reading settings for an app from text file

24 views (last 30 days)
I have designed an app for which I need to store some settings in a text file as shown in the attached example_file.ini.
In my mind the content of the file should be read into a multilevel struct with the fieldnames defined in the .ini file.
From this example the output should be as written in code below, but I can't seem to get my head around how to create a reader function for it. If you have any ideas please share them in comments - or if you have a link to a fileexchange function (of if you're making a custom one for me) which does exactly that, please submit as an answer.
If I'm completely off track and you have a brilliant other solution how to create customizable and readable defaultsettings for various properties in an app for someone who doesn't understand the app designer (or actually the app will be compiled and this settings file needs to be 'besides it') - let me know in comments.
settingsstruct.Project = struct;
settingsstruct.Users = struct;
settingsstruct.Result = struct;
settingsstruct.Project.Subproject = struct;
settingsstruct.Project.Subproject.ID12345 = struct;
settingsstruct.Project.Subproject.ID12345.datafile = "thisfilecontainsdata.mat";
settingsstruct.Project.Subproject.ID12345.Interpreter = struct;
settingsstruct.Project.Subproject.ID12345.Interpreter.decimal = ".";
settingsstruct.Project.Subproject.ID12345.Limits = struct;
settingsstruct.Project.Subproject.ID12345.Limits.datasize1 = 100;
settingsstruct.Project.Subproject.ID12345.Limits.datasize2 = 10000;
settingsstruct.Project.Subproject.ID23456 = struct;
settingsstruct.Project.Subproject.ID23456.datafile = "thisfilecontainssomeotherdata.mat";
settingsstruct.Project.Subproject.ID23456.Interpreter = struct;
settingsstruct.Project.Subproject.ID23456.Interpreter.decimal = ",";
settingsstruct.Project.Subproject.ID23456.Limits = struct;
settingsstruct.Project.Subproject.ID23456.Limits.datasize1 = 10;
settingsstruct.Project.Subproject.ID23456.Limits.datasize2 = 100000;
settingsstruct.Users.Admin = ["Admin1""Admin2"];
settingsstruct.Users.Operator = ["Operator1""Operator2""Operator3"];
settingsstruct.Result.Averaging = struct;
settingsstruct.Result.Averaging.AvgN = 3;

Accepted Answer

Mohammad Sami
Mohammad Sami on 2 Oct 2020
It would be easier if your text file complies with existing standard, such as JSON or XML. Matlab has built in function for JSON "jsondecode" and in r2020b introduced readstruct function to load XML files. Prior to that you can find function to read XML from file exchange.
  1 Comment
Johannes Hougaard
Johannes Hougaard on 5 Oct 2020
Thank you Mohammad. Using json (with jsonencode/jsondecode in MATLAB) was exactly the way to go.
I took an earlier attempt using XML but that wasn't ideal for me :(
But JSON seems to be a very good option for me.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by