Bababarghi
Bababarghi

Reputation: 581

Change the color of specific letter in console

I am forming an specific string using several strcat and displaying it into console. This string contains characters such as: 1,2,3,4,5,6,7,8,9,0,#,*,E and am using fprintf('%s') for this purpose.

For instance:

2E4137E65922#

is a possible outcome of the code.

Is there anyway I could make letter E to stand out in my output? Like making it red?

Upvotes: 6

Views: 1280

Answers (3)

Ander Biguri
Ander Biguri

Reputation: 35525

Thanks @Dev -iL for this information!

While it seems that cprinf() from my other answer does not work for single characters, if there is a single color that one wants to use, and that color is orange, then this trick used for warning in cprintf can be used:

disp(['this is [' 8 'orange]' 8 ' text'])

Read more at: http://undocumentedmatlab.com/blog/another-command-window-text-color-hack

Thus, your code would look like:

s='2E4137E65922#';
C=strsplit(s,'E');
str=C{1};
for ii=2:size(C,2)
    str=[str ['[' 8 'E]' 8 ]];
    str=[str C{ii}];
end
disp(str);

enter image description here

Upvotes: 3

Ander Biguri
Ander Biguri

Reputation: 35525

Unfortunatedly, there is no official way of doing this. However, you could use Yari Altman's cprintf(). It abuses of undocumented features of Matlab to do exactly what you want.

You can read more in the famous Undocumented Matlab blog he runs.

The example image in FEX looks like this:

enter image description here

EDIT: Theoretically, if cprintf would work as expected, the following should work:

C=strsplit(s,'E');
cprintf('black',C{1});
for ii=2:size(C,2)
    cprintf('err','E');
    cprintf('black',C{ii});
end
cprintf('black','\n');

However, in Matlab 2014b it doesnt give good results. I found out that of it doesnt work properly when there is a single character to format.

If you substitute 'E' by 'EE' works....

EDIT2: I left a comment to Yari Altman. Hopefully he will, if he can, fix the thing.

Upvotes: 7

Luis Mendo
Luis Mendo

Reputation: 112659

You can use the HTML tags <strong>, </strong> to type specific letters in bold:

str = '2E4137E65922#'; %// input string
letter = 'E'; %// letter that should be made bold
strBold = regexprep(str, letter, ['<strong>' letter '</strong>']); %// output string
disp(str)
disp(strBold)

Upvotes: 5

Related Questions