; January 2012 | Michael Ian Lapsley

[Matlab] Adding ASCII Charecters for abvanced ploting

Some times in Matlab one may want to add some charecter in the code. Here is an example:

Lets say you want to plot several variables with similar names (like A1, A2, A3) using a for loop as such:

A1 = 0:5;
A2 = 5:10;
A3 = 10:15;
for i = 1:3
    eval(['plot(A',num2str(i),')'])
    hold on
end

This will work but all of your plots will have the same line type, symbol color etc. Now it you want to add different colors to a plot you can use strings to define the colors. But using the eval function creates problems because you must create a string within a string. usually you can use this notation:

String = 'Here is another "string" in this string'

However with the eval function this does not work for me. However, ASCII charecters can be created using the char command. So to add colors to the plot:


A1 = 0:5;
A2 = 5:10;
A3 = 10:15;

colors = [1 0 0; 0 1 0; 0 0 1];
for i = 1:3
    eval(['plot(A',num2str(i),',',char(039),'color',char(039),',colors(i,:))'])
    hold on
end

A list of ASCII character codes are found here:
http://www.asciitable.com/

use the char() command to insert the charecters

Here is a good list of colors and symbols to use with these plots:

symbol = {'o','s','^','v','<','>','d','p'};
colors = [  0   0   0
            0.8 0   0
            1   0   0
            1   0.6 0
            0   0.6 0
            0   0   1
            0   0   0.6
            0.8 0   0.8
                        ];