Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define custom color shortcuts (like 'r', 'g', 'b', 'k' etc) in MATLAB

Tags:

colors

matlab

I wonder whether is it possible to define custom color shortcuts in MATLAB.

It is possible to use r instead of specifying [1,0,0] in MATLAB? Similarly, is it possible to define another shortcut?

For example, I would like to define,

[0.9047,0.1918,0.1988] as rr
[0.2941,0.5447,0.7494] as bb etc

like image 305
Denny Alappatt Avatar asked Jan 10 '23 20:01

Denny Alappatt


1 Answers

Simply put: Yes and no. You can create custom colour shortcuts like you have said, but the only way I can see you creating these shortcuts is by associative arrays / dictionaries. This may not be what you originally intended, but this is the closest thing I can think of to achieve what you're looking for. You can't create strings like r that will resolve itself to the tuple of [1,0,0] (... at least not to my knowledge), but you can create a dictionary of colour tuples where you access the dictionary by a single character or a string of characters, and the output would be a 3 element array.

With this, use a containers.Map object, and the key type would be a string (like rr, bb, etc.), and the output (value type) would be a double array. As an example, let's say your array was called colourMap. You would then initialize it, and throw some entries in like so:

%// 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 colour tuple you want by doing:

colourMap(s)

s would be the string that you want. I don't know what you want to use this for, but I'm assuming you may want to customize the colour of a plot. For example, we can do this:

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

This will create a plot from 1 to 5 for both x and y, and colour the map with the colour tuple stored in bb.

This is the only way I can see you creating customized string shortcuts. FWIW, MATLAB already has built-in colours that you can use to plot your data. For example, if you wanted to plot a red line, you would simply do:

plot(1:5, 1:5, 'r');
like image 181
rayryeng Avatar answered Jan 22 '23 18:01

rayryeng