Reading settings for an app from text file
7 views (last 30 days)
Show older comments
Johannes Hougaard
on 2 Oct 2020
Commented: Johannes Hougaard
on 5 Oct 2020
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;
0 Comments
Accepted Answer
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.
More Answers (0)
See Also
Categories
Find more on Environment and Settings 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!