how to choose a certain color for a string?

I have a text document which contains the word 'hello' how could I write this word in blue,or generally how to set a color for a string in matlab

Antworten (2)

Image Analyst
Image Analyst am 6 Apr. 2016

0 Stimmen

Use the color option to place your string onto your axes:
text(x, y, yourString, 'Color', 'r');
Color can also be a 3 number array for [r,g,b] with values in the range 0-1 to get any color you want. You can also change font size and font name with appropriate options,

5 Kommentare

jojototo
jojototo am 10 Apr. 2016
Thanks ,but I'm asking about a string in a document,do you have any ideas?
Image Analyst
Image Analyst am 10 Apr. 2016
Oh, I misunderstood. I thought you were just getting the strings from a document and then you were using MATLAB to display them somewhere, like an axes. If you actually want MATLAB to control your other program, such as Microsoft Word, and then tell Word to change the text properties (color), then you'll have to use ActiveX. Word obviously understands ActiveX but I don't know what program you're using. I'm attaching a demo where I use ActiveX to control Excel. Obviously Excel will have different methods in it and you'll have to figure out what methods Word uses. One of the easiest ways to figure it out is to record a macro "View/Macros/Record Macro..." and then edit the macro and look at the code that got recorded. Then transfer that code back to your MATLAB function.
jojototo
jojototo am 5 Jun. 2016
thanks,but I am new in programming, what do you mean "record a macro" these steps are in matlab,or this is a matlab function?
Image Analyst
Image Analyst am 5 Jun. 2016
When I said "View/Macros/Record Macro..." that is doing that from Microsoft Word. Just look at the tool ribbon in Word.
jojototo
jojototo am 8 Jun. 2016
thank you so much,I WILL TRY IT

Melden Sie sich an, um zu kommentieren.

Walter Roberson
Walter Roberson am 6 Apr. 2016

0 Stimmen

If you need to set the color inside the text document then you cannot do that: plain text does not have a color.
Changing the color of text can be done in some human-readable formats such as RTF; for example (thanks to pindari.com)
{\rtf1\ansi\deff0
{\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
This line is the default color\line
\cf2
This line is red\line
\cf1
This line is the default color
}
You might be able to set color in Excel files, under some conditions.
If the goal is to write to the command window rather than to save to a file, then see File Exchange

4 Kommentare

jojototo
jojototo am 10 Apr. 2016
Thank you so much for your reply,when I use a document instead of a text,does there any way to change the color of a certain word in that document or in other words,a way to set the color of a word like"excellent" in that document to dark gray or red or any other color......appreciating your help
Walter Roberson
Walter Roberson am 10 Apr. 2016
What is a "document" for your purpose? I was able to find the list of formatting codes for WordPerfect, for example, but it would take me more digging to find the list of supported colors.
jojototo
jojototo am 5 Jun. 2016
I mean a word document, sorry for my late reply,
per isakson
per isakson am 8 Jun. 2016
Bearbeitet: per isakson am 8 Jun. 2016

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 6 Apr. 2016

Bearbeitet:

am 8 Jun. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by