MATLAB Answers

Save and load alternative sets of preferences

2 views (last 30 days)
Matt J
Matt J on 20 Nov 2020
Edited: Matt J on 2 Dec 2020
I know how to set Preferences in Matlab through GUI buttons,
however, suppose I have different sets of preferences that I would like to switch back and forth between. Is there a way to save a given set of preferences to a file and reload it later?


dpb on 20 Nov 2020
I've never tried messsing with stuff programmatically, either, but check out
It looks as though one could build some tools to do something like you're thinking. AFAICT, however, there isn't any prebuilt feature as one would think could be to just swap sets.
Star Strider
Star Strider on 20 Nov 2020
in a script or Command Window will show the directory to the preferences that you can then bring up (with File Explorer in Windows recent releases) that you can then manually save to a different directory of your choice. Jan Simon posted a Question (with solution) in that respect several years ago, however that solution no longer works (when I tried it) in more recent releases.
Re-creating these by copying the contents of that ‘back-up’ directory back to the prefdir directory doesn’t always work either, as I unfortunately discovered after (yet another too frequent) recent Windows crash. As dpb notes, I doubt that you could do that programmatically.
It might be (I hope would be) worthwhile to contact MathWorks and submit this as an enhancement request. I (for one) would certainly want that ability, even if only to securely restore existing preferences after a crash.

Sign in to comment.

Accepted Answer

Mario Malic
Mario Malic on 20 Nov 2020


Show 2 older comments
Sean de Wolski
Sean de Wolski on 1 Dec 2020
You may want to call struct on the settings groups you're interested in ;)
Rik on 1 Dec 2020
Be warned that settings may not actually return the value you're interested in if you haven't previously opened the preferences tab that contains the setting. I learned about this when playing around with Matlab Schemer, but it applies to all settings. Hardly anything is documented, and you need to resort to digging into internals by calling struct on objects. I like that it is possible, but it is in stark contrast with the usual level of documentation. As far as I am aware the tool has never been properly documented.
Programatic access to preferences is slightly better in Matlab than Octave, but only because there it is next to impossible. I'm planning to include the monstrosity of a function below in my minify function.
function pref=GetMaxLineLengthPref(varargin)
% Retrieve the maximum line length from the editor preferences. This isn't stored this in a
% persistent, in order to allow the user to change this between function calls.
% As far as I can tell there is no way to retrieve this programatically for Octave, so the number
% mentioned in the style guide is used ( ).
% This only works if the user has _ever_ opened the preference page. In general not opening the
% preference page will either result in the returned value being 0, or an empty value. For that
% reason a list of default values is included. This list may not be correct or complete, but it
% should be close enough, especially if the user doesn't care enough to have ever opened the
% preferences page.
persistent legacy default
if isempty(legacy)
legacy.isOctave=exist('OCTAVE_VERSION', 'builtin');
if legacy.isOctave
elseif ifversion('<=','R2010a')
% The default for R2010a and R14 seems to be 75.
% As far as I can tell the default is 80 at least since R2011a.
if legacy.isOctave
elseif legacy.settings
s = settings;
elseif legacy.R13'EditorMaxCommentWidth');%#ok<JAPIMATHWORKS>
else % If you are using R12 or before, you have a lot more issues than just this.
if isempty(pref) || pref==0
Matt J
Matt J on 2 Dec 2020
My ultimate goal was to make a temporary change to the background color in the desktop tools as described here,
except that I would like to do it programmatically or at least with fewer GUI interactions. There doesn't appear to be a SettingsGroup that handles this, however, nor a QuickAccess option.

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!