필터 지우기
필터 지우기

Reading settings for an app from text file

조회 수: 2 (최근 30일)
Johannes Hougaard
Johannes Hougaard 2020년 10월 2일
댓글: Johannes Hougaard 2020년 10월 5일
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;

채택된 답변

Mohammad Sami
Mohammad Sami 2020년 10월 2일
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
Johannes Hougaard
Johannes Hougaard 2020년 10월 5일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by