matlab在终端显示有颜色的字(cprintf函数的用法)

来源:互联网 发布:软件编程职业培训 编辑:程序博客网 时间:2024/04/29 16:53

     我们知道在matlab中显示文字用display,但它显示出来的颜色默认是黑色字体,我们要相在终端显示有颜色的字体怎么办呢?

    有以下两种方法供参考:

    

    1.用fprintf函数如下:

     >>fprintf(2,'红颜色\n')

      红颜色

    不过它有个缺点是只能显示这种error的红颜色

 

    2.cprintf函数,这是国外的一个叫Yair M. Altman的人写的程序,它的博客地址为:

    这个函数的下载地址为:(下载后将放入当前程序运行目录下就可以调用了)

   点击下载(download)

 

    它的用法我来简单翻译一下:

    语法:   count = cprintf(style,format,...)

    描述:   CPRINTF 用指定格式(style)来处理指定的文档

            可用的格式名为

            'Text'                 -- 默认:黑色black

            'Keywords'            -- 默认:蓝色 blue

            'Comments'           -- 默认:绿色green

            'Strings'              -- 默认:紫色 purple

            'UnterminatedStrings'   -- 默认:暗红 dark red

            'SystemCommands'     -- 默认:橘色 orange

            'Errors'               -- 默认:淡红 lightred

            'Hyperlinks'           -- 默认:带下划线的蓝色underlined blue

 其他颜色如下

'Black','Cyan','Magenta','Blue','Green','Red','Yellow','White'

    注意: 以'-'开始的是下划线,例如:

          '-Blue' 是蓝色的下划线,与'Hyperlinks'相似

   STYLE 也接受通用的RGB向量做为参数,在它前面加负号,表示下划线,例如:

          -[0,1,1]表示带下划线的蓝绿色cyan

   STYLE 不区分大小写 (case-insensitive),接受唯一的部分字符串


一些实用例子:

 

附原文帮助文件:

或参照http://www.mathworks.com/matlabcentral/fileexchange/24093

% CPRINTF displays styledformatted text in the Command Window

%

% Syntax:

%    count = cprintf(style,format,...)

%

% Description:

%    CPRINTF processes the specified text usingthe exact same FORMAT

%    arguments accepted by the built-in SPRINTFand FPRINTF functions.

%

%    CPRINTF then displays the text in theCommand Window using the

%    specified STYLE argument. The acceptedstyles are those used for

%    Matlab's syntax highlighting (see: File /Preferences / Colors /

%    M-file Syntax Highlighting Colors), andalso user-defined colors.

%

%    The possible pre-defined STYLE names are:

%

%       'Text'                 - default: black

%       'Keywords'             - default: blue

%       'Comments'             - default: green

%       'Strings'              - default: purple

%       'UnterminatedStrings'  - default: dark red

%       'SystemCommands'       - default: orange

%       'Errors'               - default: light red

%       'Hyperlinks'           - default: underlined blue

%

%      'Black','Cyan','Magenta','Blue','Green','Red','Yellow','White'

%

%    Note: styles beginning with '-' will beunderlined. For example:

%          '-Blue' is underlined blue, like'Hyperlinks';

%          '-Comments' is underlined green etc.

%

%    STYLE also accepts a regular Matlab RGBvector, that can be negated

%    for underlining. For example: -[0,1,1]means underlined cyan.

%

%    STYLE is case-insensitive and acceptsunique partial strings just

%    like handle property names.

%

%    CPRINTF by itself, without any inputparameters, displays a demo

%

% Example:

%    cprintf;  % displays the demo

%    cprintf('text',   'regular black text');

%    cprintf('hyper',  'followed %s','by');

%    cprintf('k',      '%d colored', 4);

%    cprintf('-comment','& underlined');

%    cprintf('err',    'elements\n');

%    cprintf('cyan',   'cyan');

%    cprintf('-green', 'underlined green');

%    cprintf(-[1,0,1], 'underlined magenta');

%    cprintf([1,0.5,0],'and multi-\nlineorange\n');

%    cprintf('string');  % same as fprintf('string') andcprintf('text','string')