R’s matplot function in MATLAB

By default, MATLAB’s plot function draws no markers in the figure that it produces. One may explicitly specify a marker and a line style following the line specification string syntax; however, only one marker type and line style may be applied to a single data set.

Further we present a simple MATLAB function that implements the same functionality as R’s matplot function and allows to set style for each data line shown.

Source code

The source code of the MATLAB implementation of matplot is given below. The function provides options typeltylwdcol and pch to customize the line type, style, width, color and the marker type, respectively.


Here we give some examples of using MATLAB’s matplot function presented above.

Default plot settings

By default, matplot does not draw lines and uses various colors and marker types for the data points.

matplot(cumsum(rand(10, 5), 2));


Data plot by matplot with its default settings.

Single line style and color

The following command specifies the same line style (solid) and color (black) for all data lines; the markers are different.

matplot(cumsum(rand(10, 5), 2), 'type', 'b', 'lty', '-', 'col', 'k');


Data plot by matplot with the single color and style for lines.

Alternate line styles and colors

The following command specifies alternate line styles (solid and dashed) and colors (black, red and green) for the data lines; the marker type (circle) is the same for all lines.

matplot(cumsum(rand(10, 5), 2), 'type', 'b', 'pch', 'o', ...

    'lty', {'-', '--'}, 'col', 'krg');


Data plot by matplot with alternate line colors and styles.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s