An Introduction to Scripts: A Script of Variables "customcolours"

This guide will instruct you on creating a simple script in MATLAB to create a list of 3 row vector variables which will be useful to call up later. This guide takes you through the basics of using the command window, workspace, current folder, variable editor and editor. If you just need the script it is available at the bottom.

Default MATLAB Colours?

All colours are made from a mixture of three primary colours:

  • red
  • green
  • blue

A colour can be expressed as a [r,g,b] row vector opposed to a pre-existing string.

MATLAB has the following strings for the primary and secondary colours:

\displaystyle \begin{array}{*{20}{c}} {\text{Specifier Long}} & {\text{Specifier}} & {\text{RGB Row Vector}} \\ {\text{black}} & \text{k} & {\left[ {0,0,0} \right]} \\ {\text{red}} & \text{r} & {\left[ {1,0,0} \right]} \\ {\text{green}} & \text{g} & {\left[ {0,1,0} \right]} \\ {\text{blue}} & \text{b} & {\left[ {0,0,1} \right]} \\ {\text{yellow}} & \text{y} & {\left[ {1,1,0} \right]} \\ {\text{magenta}} & \text{m} & {\left[ {1,0,1} \right]} \\ {\text{cyan}} & \text{c} & {\left[ {0,1,1} \right]} \\ {\text{white}} & \text{w} & {\left[ {1,1,1} \right]} \end{array}

Black is shown in the absence of these three colours and white is shown when these three colours are at full brightness.

8 Bit 0:255

Many programs such as Microsoft Office list colors in 8 bit, which means they list \displaystyle {{2}^{8}}=256 levels. If we subtract 1 from 256 to get a 0 value with range from 0-255. We may use the colour pickers in Microsoft Word to determine the colour we want to use. Select the button to change the colour of the text, select More Colours:

Select your colour and then select custom:

You now get your RGB values listed from 0 to 255.

We can create a row vector using the three numbers shown above however we need to normalise them dividing through by 255.

mw_wine=[192/255,0/255,0/255]

Now we get the variable mw_wine we can double click on it, in the workspace to view it in the variable editor:

We are now going to close down the variable editor.

Creating a Script File

Now we are going to create a new script in the current folder:

Called

customcolours

This will open up the Editor:

We can now type in a comment and the same line as code as before:

Now how the file name in the editor shows ending in a * this * means the file is unsaved. When we save the file the * will disappear:

If we clear all variables using the command

clear all

And clear the command window using the command

clc

Now we have no variables open in the workspace or any command history:

Now we can type in the name of the script

customcolours

The variable mw_wine is created and shows in the workspace and in command window history:

Now we can add additional colours

mw_wine=[192/255,0/255,0/255]
mw_red=[255/255,0/255,0/255]
mw_orange=[255/255,0/255,0/255]
mw_yellow=[255/255,255/255,11/255]
mw_lime=[146/255,208/255,80/255]
mw_green=[0/255,176/255,80/255]
mw_sky=[3/255,177/255,240/255]
mw_blue=[59/255,145/255,207/255]
mw_navy=[0/255,32/255,96/255]
mw_purple=[114/255,51/255,161/255]
mw_grey=[116/255,116/255,116/255]

When we launch customcolours we see all these variables listed:

Creating a Second Script and Calling Up the First Script

We can now create a second script called testscript

In the first line we will leave a comment and then call up the first script

customcolours

We will then generate a simple line plot (more details later) and colour it with one of the colours from customcolours

We can now change the colour and relaunch the code:

And change it again:

Of course we don't need the names of all the colours to flood the command window when we call up customcolours to prevent this we can end the lines in a semicolon

If we now run customcolours (this time by right clicking the script file and selecting run):

We see all the variables created but the command window is clean:

Script

save as customcolours.m

[sourcecode language="matlab"]
%% This script will load custom colours from Microsoft Word
mw_wine=[192/255,0/255,0/255]
mw_red=[255/255,0/255,0/255]
mw_orange=[255/255,0/255,0/255]
mw_yellow=[255/255,255/255,11/255]
mw_lime=[146/255,208/255,80/255]
mw_green=[0/255,176/255,80/255]
mw_sky=[3/255,177/255,240/255]
mw_blue=[59/255,145/255,207/255]
mw_navy=[0/255,32/255,96/255]
mw_purple=[114/255,51/255,161/255]
mw_grey=[116/255,116/255,116/255]
[/sourcecode]

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.