How to define custom color combinations (like "r", "g", "b", "k", etc.) in MATLAB

I wonder if it is possible to define custom color labels in MATLAB.

Can I use r

instead of specifying [1,0,0]

in MATLAB? Likewise, is it possible to define another label?

For example, I would like to define

[0.9047,0.1918,0.1988]

how rr


[0.2941,0.5447,0.7494]

like bb

etc.

+3


source to share


2 answers


Use . struct

%Defining your colors and some other colors in a struct
c = struct('rr', [0.9047, 0.1918, 0.1988], ...  %Your required color
    'bb', [0.2941, 0.5447, 0.7494], ... %Your required color
    'um', [0.0824, 0.1294, 0.4196], ... %ultra marine
    'br', [0.6510, 0.5725, 0.3412], ... %bronze
    'gl', [0.8314, 0.7020, 0.7843] );   %greyed lavender

      



Now to use these colors use the Color ie property

plot(x, y, 'Color', c.br);  %Using the color 'bronze' defined in the struct

      

+2


source


Simply put: yes and no. You can create custom color labels as you said, but the only way to see how you create these labels is with associative arrays / dictionaries. This may not be what you originally planned, but it is the closest thing I can think of to achieve what you are looking for. You cannot create strings such as r

those that resolve themselves in a tuple [1,0,0]

(... at least as far as I know), but you can create a dictionary of color tuples where you access the dictionary with a single character or string of characters, and the output will be an array of 3 elements.

When using this object containers.Map

, and the type of key is a string (e.g. rr

, bb

etc.), and the output (type value) will be a double array. As an example, let's say your array has been called colourMap

. Then you initialize it and throw some entries like this:

%// Initialize associative array
colourMap = containers.Map('KeyType', 'char', 'ValueType', 'any');

%// Put some entries in - referring to your post
colourMap('r') = [1 0 0];
colourMap('rr') = [0.9047,0.1918,0.1988];
colourMap('bb') = [0.2941,0.5447,0.7494];

      

Once you set this up, you can access the specific color tuple you want:

colourMap(s)

      



s

will be the string you want. I don't know what you want to use this for, but I am assuming you can customize the plot color. For example, we can do this:

plot(1:5, 1:5, 'Color', colourMap('bb'));

      

This will create a 1 to 5 plot for x

and y

and color the map with the color tuples stored in bb

.

This is the only way to see how you create custom string shortcuts. FWIW, MATLAB already has built-in colors that you can use to plot your data. For example, if you want to draw a red line, you simply do:

plot(1:5, 1:5, 'r');

      

+8


source







All Articles