Coloring string, fprintf seems limited and cprintf is even a thing?

Hello there,
I am trying to modify a pass/fail function which prints the results in MS word template. Right now it just print 'Pass' in black and white formet but I want to color it as green.
How can I color the text ?
function reportgen_replace(hfind, fromstr, tostr)
%The argument tostr return a string name Pass
% Execute the replacement
if strcmp(tostr, 'Pass') %I made this function just to give a try but it does not seem to work
ColorPF = fprintf(2,'Pass'); %Although I need green color, I know that this function return text in red so I just wanted to give a shot
invoke(hfind, 'Execute', fromstr, 0, 0, 0, 0, 0, 1, 1, 0, ColorPF, 2);
end
%instead of printing any string, it return values (4

Answers (1)

cprintf is a File Exchange contribution. You can install it using the Add-On Explorer.
The return value from fprintf() is the number of bytes converted, not the text.

7 Comments

If I use that add-on explorer, anything I change would it still visible to the MSWoRD?
Because I have a Template on MS Word where Iam pasting the result from MATLAB.
No, cprintf() relies on tricks and internal representations. It is designed only to output color and style to the MATLAB Command Window. It makes no attempt to generate some kind of standardized representation that MS Word might understand, because the Command Window does not operate based on standard representations.
There is no text structure or escape sequence that you can build at the MATLAB level that will be interpreted as color information by MS Word. Instead you will need to use MATLAB on MS Windows, and you will need to use active X or .NET to form a connection to an MS Word server, and you will need to build Selections and change the ColorIndex of the Selection;
Oh no! So is there any way to color them when they will go to word for filling?
If you're using the report generator, look into the mlreportgen.dom.Color class to control color.
The 9112 File Exchange contribution shows working with ActiveX to talk to Word. Notice in particular the part of the example that does
WordText(ActXWord,TextString,Style,[0,1],'wdRed');%red text and enter after text
and then look down to the WordText function there which does
if(nargin == 5)%check to see if color_p is defined
actx_word_p.Selection.Font.ColorIndex=color_p;
Hey Walter, it seemed like the exact thing that I need to learn now. Could you give me some good keyword, source or any source to learn about it fully in depth? I would greatly appreciate it.
is wordtext a function by user defined?
WordText(ActXWord,TextString,Style,[0,1],'wdRed');
WordText is defined in the SUB-FUNCTIONS portion of the 9112 File Exchange contribution.

Sign in to comment.

Categories

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

Tags

Asked:

on 20 Mar 2023

Commented:

on 30 Mar 2023

Community Treasure Hunt

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

Start Hunting!