Save and load alternative sets of preferences

23 ビュー (過去 30 日間)
Matt J
Matt J 2020 年 11 月 20 日
編集済み: Matt J 2020 年 12 月 2 日
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?
  2 件のコメント
dpb
dpb 2020 年 11 月 20 日
I've never tried messsing with stuff programmatically, either, but check out https://www.mathworks.com/help/matlab/ref/settings.html
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 2020 年 11 月 20 日
Executing:
prefdir
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.

サインインしてコメントする。

採用された回答

Mario Malic
Mario Malic 2020 年 11 月 20 日
  5 件のコメント
Rik
Rik 2020 年 12 月 1 日
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 com.mathworks.services.Prefs 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 ( https://wiki.octave.org/Octave_style_guide ).
%
% 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)
%#dependencies{ifversion}
legacy.isOctave=exist('OCTAVE_VERSION', 'builtin');
legacy.settings=ifversion('>=','R2018a');
legacy.com.mathworks=ifversion('>=','R13');
legacy.R13=ifversion('==','R13');
if legacy.isOctave
% https://wiki.octave.org/Octave_style_guide
default=80;
elseif ifversion('<=','R2010a')
% The default for R2010a and R14 seems to be 75.
default=75;
else
% As far as I can tell the default is 80 at least since R2011a.
default=80;
end
end
if legacy.isOctave
pref=0;
elseif legacy.settings
s = settings;
pref=s.matlab.editor.displaysettings.linelimit.LineColumn.ActiveValue;
elseif legacy.com.mathworks
pref=com.mathworks.services.Prefs.getIntegerPref('EditorRightTextLineLimit');%#ok<JAPIMATHWORKS>
elseif legacy.R13
pref=com.mathworks.services.Prefs.getIntegerPref('EditorMaxCommentWidth');%#ok<JAPIMATHWORKS>
else % If you are using R12 or before, you have a lot more issues than just this.
pref=80;
end
pref=double(pref);
if isempty(pref) || pref==0
pref=default;
end
end
Matt J
Matt J 2020 年 12 月 2 日
編集済み: Matt J 2020 年 12 月 2 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming Utilities についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by