How to set ini value with INI Config

Hi,
I am using INI Config to read from and write to a custom .ini-file. The reading works perfectly, I use the GetValues-Function for that.
However, I cannot get the SetValues-Function to work. I do not get an Error but the value in my .ini-file does not change either.
[~] = ini.SetValues('Category','SettingA','Teststring');
Nothing happens. Does anybody know what I am doing wrong here?
Thanks in advance!

4 commentaires

Michael - please clarify what you mean by nothing happens. Are you trying to update an existing key with a new value? Does your section 'Category' exist? Or is it not saving the update to file (I suspect you have to call ni.WriteFile('example2.ini') to save your changes.
Also, what is the return value from your above call? For example,
[status] = ini.SetValues('Category','SettingA','Teststring');
what is status? If the "set" works, then this value will be 1. Else, if it fails, it will be a 0.
Please note that I was able to call **SetValues* and update an existing key within the section...
Thanks Goeff!
By nothing happens I meant, that I do not get an error message but also no change appears in the .ini-file. In Debug-Steps I could see that MATLAB is running the commands though.
The status is 1 for success.
What did the trick is this:
ini.WriteFile('mysettings.ini');
However, before WriteFile my .ini-file looks something like this:
; Settings File for my App
; Created by MC on 20181122
[Category] ; Settings required for App
SettingA = ABC ; Explanation for this Key
After WriteFile my .ini-file looks like this:
Settings File for my App; Created by MC on 20181122[Category] ; Settings required for AppSettingA = ABC ; Explanation for this Key
All the Linebreaks are gone, rendering this file useless as the Keynames get joined with the previous line end. I assume there is again something I am doing wrong.
Please advise.
Geoff Hayes
Geoff Hayes le 22 Nov 2018
MIchael - could you paste the code that you have written that creates the ini file (with default settings) and then the code that updates the file (which "corrupts" it).
Michael Critchfield
Michael Critchfield le 22 Nov 2018
Modifié(e) : Michael Critchfield le 23 Nov 2018
Hi Goeff,
Thanks for getting back to me on this.
I did not use code to create my ini file, I typed it manually.
Reading the settings works fine.
Thanks to your advice, writing works fine now too, except the missing linebreaks.
I use the following lines to update a setting in my ini file:
ini = IniConfig();
[~] = ini.ReadFile('mysettings.ini');
SettingA = string(ini.GetValues('Category','SettingA'));
% ...
SettingA = 'Teststring';
[~] = ini.SetValues('Category','SettingA',SettingA);
[~] = ini.WriteFile('mysettings.ini');
clear ini;

Connectez-vous pour commenter.

Réponses (0)

Catégories

Produits

Version

R2017a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by