addSetting
Adds a setting, a named value holder, to a settings group.
Category: Environment and Settings > System Commands
Syntax
S = addSetting(G, name)
Creates an empty setting called name in group G.
S = addSetting(G, name, Name, Value)
Creates the setting with options such as 'PersonalValue' and 'ValidationFcn'.
Description
A setting holds up to four layers of value: temporary, personal, installation and factory. Reading S.ActiveValue returns the first one that is set, in that priority order, and errors when none is. Assigning S.PersonalValue or S.TemporaryValue stores a value in that layer and runs the validation function.
A new setting starts with no value in any layer, so give it a personal value before reading it.
Input arguments
G- A
SettingsGroupcreated withaddGroup. Settings cannot be added to the root returned bysettings. name- Setting name, with the same naming rules as a group.
'PersonalValue'- Initial personal value. It is checked by the validation function, if any.
'ValidationFcn'- Function handle called on every assigned value; it should error for invalid values, like
@matlab.settings.mustBeNumericScalar. 'Hidden'- Logical flag that hides the setting from listings.
Output arguments
S- The new
matlab.settings.Settingobject.
Example
Setting with validation
s = settings;
g = addGroup(s, 'demo');
a = addSetting(g, 'Size', 'PersonalValue', 10, ...
'ValidationFcn', @matlab.settings.mustBeNumericScalar);
disp(a.ActiveValue)
a.PersonalValue = 20;
disp(a.ActiveValue)
try
a.PersonalValue = 'x';
catch err
disp(['Rejected: ' err.message])
end
removeGroup(s, 'demo');
10
20
Rejected: Error setting 'Size' in group 'demo': Value must be numeric.
Notes
In MatLivre, personal values last for the current session only; they are not saved between runs as MATLAB saves them.