Get from Ico-github-logo

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Color Palette Tables (.cpt) for Matlab

  • cptcbar(ax, map, loc, fla...
    Create colorbar associated with cpt colormap
  • cptcmap(varargin)
    CPTCMAP Apply a .cpt file as colormap to an axis
  • minmax(a, type, whis)
    MINMAX Returns minimum and maximum value in the given array
  • parsepv(Param, pvpairs, v...
    PARSEPV Parses parameter/value pairs
  • View all files
5.0 | 12 ratings Rate this file 49 Downloads (last 30 days) File Size: 1.43 MB File ID: #28943 Version: 1.7
image thumbnail

Color Palette Tables (.cpt) for Matlab


Kelly Kearney (view profile)


04 Oct 2010 (Updated )

Create and apply GMT-style colormaps in Matlab

| Watch this File

File Information

cptcmap.m: color palette tables (.cpt) for Matlab


This file inspired Cmap2pal Convert Matlab Colormap To Binary .Pal Format, Ibcso International Bathymetric Chart Of The Southern Ocean Toolbox, Dem Viewer, Cubehelix Colormaps: Beautiful, Distinct, Versatile!, Color Brewer: Attractive And Distinctive Colormaps, and Cmocean Perceptually Uniform Colormaps.

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
16 Nov 2016 Lei Cheng

Thank you so much.! It is awesome!

17 May 2016 wkillers

10 Apr 2016 ashraf rateb

helpful, thank you

23 Mar 2016 Matteo Bagagli

Great tool!
Instead I can't figure out how to obtain shading interp. Matlab continues to warning about it. Any ideas please?

01 Sep 2015 E. Cheynet

29 Jul 2015 Chad Greene

Chad Greene (view profile)

29 Jul 2015 Chad Greene

Chad Greene (view profile)

Superb. Thanks for writing this easy-to-use, well-documented function.

Comment only
07 Oct 2013 Kelly Kearney

Kelly Kearney (view profile)

I've addressed Seth's question via email. But in case others encounter this, the behavior he was seeing is as intended. While the length of the resulting colormap can be specified via the ncol input, the number of unique colors in that colormap is determined by the .cpt file itself. The GMT_Haxby colormap defines 32 specific colors, so increasing the resolution of the colormap via ncol will create a larger colormap array, but many of these values will simply be replicates.

Comment only
05 Oct 2013 Seth

Seth (view profile)

More detail on my comment below:

cptcmap('GMT_Haxby','ncol', 4) produces a contour plot with four colors;
cptcmap('GMT_Haxby','ncol', 8) produces a contour plot with eight colors;
cptcmap('GMT_Haxby','ncol', 32) produces a contour plot with thirty-two colors;

However, cptcmap('GMT_Haxby','ncol', 256) produces a contour plot with 32 colors, not 256 colors. Any number above 32 leads to a plot with just 32 colors.

Matlab novice, hope I'm not missing something trivial!

Comment only
04 Oct 2013 Seth

Seth (view profile)

I'm having trouble getting these contour plots to use more than ~32 colors with this function.

If I use the command:


then contour plots plot using the jet colorscheme with 256 colors. However, if I use the command:

cptcmap('GMT_Haxby','ncol', 256);

then all of the subsequent contour plots display with only ~32 colors.

Any suggestions? Thanks!

Comment only
16 Apr 2013 Claire

Claire (view profile)

Function works brilliantly!! Thanks so much!

28 Mar 2013 Kelly Kearney

Kelly Kearney (view profile)


Sorry, I always forget that roundn is part of the Mapping toolbox. Just replace the roundn call:

space = roundn(space, -3);


space = round(space*1e3)/1e3;

Comment only
27 Mar 2013 Claire

Claire (view profile)

I don't have access to the "roundn" function. Is there a work around so that I can get your function working?

Comment only
03 Jan 2013 Frank Engel

Excellent and very useful. Works as expected, and had no problems using with gradients provided from cpt-city (

01 Oct 2012 Jesse Hansen

Very useful for bathymetric mapping. Please note that there is a dependency on the mapping toolbox due to the use of the 'roundn' function. This could easily be replaced by 1 line of standard Matlab code.

16 Jul 2012 angela

angela (view profile)

28 Mar 2012 Kelly Kearney

Kelly Kearney (view profile)

@camilo melo,

I don't have R20011b, so I can't test it, but I can't think of any reason it wouldn't work in that version. Based on the error message you mention, my guess is you tried to call the function without any input arguments; it needs at least one (the name of the colormap).

Comment only
27 Mar 2012 camilo melo

hi, does this function works for MATLAB R2011b?? i tried to use it but got this error message Index exceeds matrix dimensions.

Error in cptcmap (line 123)
[blah, blah, ext] = fileparts(varargin{1});

Comment only
03 Apr 2011 Felipe G. Nievinski

Another one, trying


Just had to replace

if ~strcmp(varargin{1}(end-3:end), '.cpt') % without extension


if (numel(varargin{1}) <= 4) || ~strcmp(varargin{1}(end-3:end), '.cpt') % without extension

Comment only
03 Apr 2011 Felipe G. Nievinski

Another one, trying


Just had to replace

if ~strcmp(varargin{1}(end-3:end), '.cpt') % without extension


if (numel(varargin{1}) <= 4) || ~strcmp(varargin{1}(minend-3:end), '.cpt') % without extension

Comment only
03 Apr 2011 Felipe G. Nievinski

You might want to replace l.83:

cptpath = './cptfiles';


cptpath = fullfile(fileparts(which('cptcmap')), 'cptfiles');

That way the source code needs not be modified when first downloading the package or later when moving the routine files around.

I was having problems trying


where elevation.cpt was downloaded from cpt-city and placed in the cptpath; it turns out that l.98,

if exist(varargin{1}, 'file') % full filename and path given

would find this other file instead:

which elevation
C:\Program Files\MATLAB\R2009b\toolbox\map\map\elevation.m

You might want to replace the block l.98-111 with the following (minimally modified):

if ~strcmp(varargin{1}(end-3:end), '.cpt') % without extension
varargin{1} = [varargin{1} '.cpt'];
if exist(varargin{1}, 'file') % full filename and path given
filename = varargin{1};
else % only file name given
filename = fullfile(cptpath, varargin{1});
if ~exist(filename, 'file')
error('Specified .cpt file not found');

Finally, not sure why the __MACOSX subdirectory is being distributed.

Comment only
26 Mar 2011 Felipe G. Nievinski

Great idea, always wanted to make my matlab maps look like gmt's. Also thanks for pointing out to cpt-city, it's a great collection.

You might want to use the acknowledgement field in the FEX form, so that your submission is back-linked from related entries, e.g., 28982 and the ones linked from there.

11 Oct 2010 Kelly Kearney

Kelly Kearney (view profile)


If by the inverse colormap, you mean you want to flip the direction of the colors, then yes, use the 'flip' option:

cptcmap('GMT_seis', 'flip', true)


Comment only
11 Oct 2010 Marios Karaoulis

Hi. Very nice package. Is it possible to use the inverse colormap of GMT_seis.cpt?

12 Apr 2011 1.2

-bug fixes where some input filenames were misinterpreted (thanks to Felipe)
-colormap display option no longer relies on external functions, has cleaner display, and renders more quickly

24 Apr 2013 1.3

Replaced roundn call with manual calculation to remove unintended dependency on Mapping Toolbox.

08 Jul 2013 1.4

Added error check in case no input is supplied. Also corrected the roundn issue (correctly, this time).

10 Jul 2014 1.5

- Corrected bug in HSV color calculation
- Updated for 2014b graphics

28 Aug 2014 1.6

Corrected bug in colormap length calculation

20 Aug 2016 1.7

- Linked to GitHub repository
- Added cptcbar.m to create colorbars
- Now allows for multiple folders holding .cpt files

Contact us