Clear Filters
Clear Filters

Setting the editor (not Live editor) text and background colors from code

6 views (last 30 days)
I am trying to make a function that allows me to quickly change the color scheme of my MATLAB editor. I found here:Access and Modify Settings, that I can use the settings groups to change my preferences from code.
I am able to set many of the color settings from code this way:
s = settings;
s.matlab.colors.KeywordColor.TemporaryValue = [0 190 190];
However, I cannot find the text and background color in here:
>> s.matlab.colors
ans =
SettingsGroup 'matlab.colors' with properties:
ValidationSectionColor: [1×1 Setting]
KeywordColor: [1×1 Setting]
CommentColor: [1×1 Setting]
StringColor: [1×1 Setting]
UnterminatedStringColor: [1×1 Setting]
SyntaxErrorColor: [1×1 Setting]
SystemCommandColor: [1×1 Setting]
commandwindow: [1×1 SettingsGroup]
programmingtools: [1×1 SettingsGroup]
>> s.matlab.colors.commandwindow
ans =
SettingsGroup 'matlab.colors.commandwindow' with properties:
HyperlinkColor: [1×1 Setting]
ErrorColor: [1×1 Setting]
WarningColor: [1×1 Setting]
>> s.matlab.colors.programmingtools
ans =
SettingsGroup 'matlab.colors.programmingtools' with properties:
HighlightAutofixes: [1×1 Setting]
VariablesWithSharedScopeColor: [1×1 Setting]
AutomaticallyHighlightVariables: [1×1 Setting]
AutofixHighlightColor: [1×1 Setting]
VariableHighlightColor: [1×1 Setting]
ShowVariablesWithSharedScope: [1×1 Setting]
CodeAnalyzerWarningColor: [1×1 Setting]
What I am looking for are these colors
How can I change these in code?

Accepted Answer

Trym Gabrielsen
Trym Gabrielsen on 12 Dec 2023
As suggested by @Rik, I had a look in the "MATLAB Schemer" package, to see how it is done there.
I found the following methods:
background = 80/255*[1 1 1]; % Dark Gray
old_background_color = com.mathworks.services.Prefs.getColorPref('ColorsBackground'); % Get the current background color
com.mathworks.services.Prefs.setColorPref('ColorsBackground',java.awt.Color(background(1),background(2),background(3)))
com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsBackground');
text = 190/255*[1 1 1]; % Light Gray
old_text_color = com.mathworks.services.Prefs.getColorPref('ColorsText'); % Get the current text color
com.mathworks.services.Prefs.setColorPref('ColorsText', java.awt.Color(text(1),text(2),text(3)))
com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsText');
However, I get the following warning:
'com.mathworks' package and subpackages will be removed in a future release. There is no simple replacement for this.

More Answers (1)

Rik
Rik on 12 Dec 2023
I always run MATLAB Schemer after I install Matlab.
You can dig into the code to find the actual field you can use. It might not be documented, but it hasn't changed in years, so you're probably good until Mathworks rolls out the new desktop as the default.
  1 Comment
Trym Gabrielsen
Trym Gabrielsen on 12 Dec 2023
Edited: Trym Gabrielsen on 12 Dec 2023
Thanks! I did not find this package when searching around for solutions for some reason...
I found these commands to change the background color and text color from the "MATLAB Schemer":
background = 80/255*[1 1 1];
old_background_color = com.mathworks.services.Prefs.getColorPref('ColorsBackground'); % Get the current background color
com.mathworks.services.Prefs.setColorPref('ColorsBackground',java.awt.Color(background(1),background(2),background(3)))
com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsBackground');
text = 190/255*[1 1 1];
old_text_color = com.mathworks.services.Prefs.getColorPref('ColorsText'); % Get the current text color
com.mathworks.services.Prefs.setColorPref('ColorsText', java.awt.Color(text(1),text(2),text(3)))
com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsText');
However, I am getting a warning that the "com.mathworks" will be outdated in a later release, without any equivalent.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!