Change the color of a specific letter in the console

I form a specific line using multiple strcat and displaying it in the console. This line contains characters such as: 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 , # , * , E and I use fprintf('%s') for this purpose.

For instance:

2E4137E65922 #

- possible outcome of the code.

Anyway, could I write an E to stand out on my output? How to make it red?

+5
source share
3 answers

Thanks @Dev -iL for this info!

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

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

More details: http://undocumentedmatlab.com/blog/another-command-window-text-color-hack

So your code will look like this:

 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

+2
source

Unfortunately, there is no official way to do this. However, you can use Yari Altman cprintf() . It abuses the undocumented Matlab features to do exactly what you want.

You can read more at the famous Matlab Unregistered Blog .

An example image in FEX is as follows:

enter image description here

EDIT: Theoretically, if cprintf works 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 does not give good results. I found out that it does not work properly when there is one character to format.

If you replace "E" with "EE", work ...

EDIT2: I left a comment for Jari Altman. I hope he can, if he can, fix it.

+6
source

You can use the HTML tags <strong> , </strong> to type special 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) 
+5
source

Source: https://habr.com/ru/post/1231944/


All Articles