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

5 次查看(过去 30 天)
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?

采纳的回答

Trym Gabrielsen
Trym Gabrielsen 2023-12-12
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.

更多回答(1 个)

Rik
Rik 2023-12-12
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 个评论
Trym Gabrielsen
Trym Gabrielsen 2023-12-12
编辑:Trym Gabrielsen 2023-12-12
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 System Commands 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by