custom colormaps in matlab

Summary

A function to easily create custom colormaps in Matlab.

There are times when the custom colormaps provided by Matlab don't cut it. An easy way to create nice, smooth gradients for custom colormaps is to use linspace's ability to create linearly spaced vectors between two arbitrary numerical values. Building on this, I made a simple function that takes a cell array of different RGB values and outputs a smooth gradient. For example, inputting a cell array with values {[1 0 0], [0 1 0],[0 0 1]} makes a gradient that gradually changes from red to green to blue. You can modify the number of points in the gradient by specifying the variable argument 'nPoints', an example usage:

download codeExample.m

Matlab M
  1. myMap = customColormap({[1 0 0], [0 1 0],[0 0 1]},'nPoints',25);
  2. colormap(myMap);

This code also takes advantage of getOptions, which I discussed previously (see dealing with variable options (varargin) in matlab) as a way to handle variable input arguments in a consistent manner.

M-files and example code below; I'll add it to a GitHub repository at some point in the future.

customColormap.m
getOptions.m
download customColormap.m

Matlab M
  1. function [outputColormap] = customColormap(colorList,varargin)
  2.         % creates a custom colormap
  3.         % biafra ahanonu
  4.         % started: 2014.01.03
  5.         % inputs
  6.                 % colorList - a cell array containing vectors with RGB values, e.g. {[1 1 1], [0 0 1],[1 0 0]}
  7.         % variable inputs
  8.                 % nPoints - numeric value specifying the number of points between each color value in the gradient
  9.         % outputs
  10.                 %
  11.  
  12.         % changelog
  13.                 % 2014.05.09 - commented to make more readable
  14.         % TODO
  15.                 %
  16.  
  17.         %========================
  18.         options.nPoints = 50;
  19.         % get options
  20.         options = getOptions(options,varargin);
  21.         % display(options)
  22.         % unpack options into current workspace
  23.         % fn=fieldnames(options);
  24.         % for i=1:length(fn)
  25.         %       eval([fn{i} '=options.' fn{i} ';']);
  26.         % end
  27.         %========================
  28.  
  29.         try
  30.                 % check that colorList was input, else return default map
  31.                 if isempty(colorList)
  32.                         colorList = {[1 1 1], [0 0 1],[1 0 0]};
  33.                 end
  34.                 nColors = length(colorList);
  35.                 redMap = [];
  36.                 greenMap = [];
  37.                 blueMap = [];
  38.                 % loop over each color in the list and append its values to the RGB map values
  39.                 for i=1:(nColors-1)
  40.                         % linspace is used to create an even gradient between the values specified
  41.                         redMap = [redMap linspace(colorList{i}(1),colorList{i+1}(1),options.nPoints)];
  42.                         greenMap = [greenMap linspace(colorList{i}(2),colorList{i+1}(2),options.nPoints)];
  43.                         blueMap = [blueMap linspace(colorList{i}(3),colorList{i+1}(3),options.nPoints)];
  44.                 end
  45.                 % concatenate the RGB map values to create a custom colormap matrix
  46.                 outputColormap = [redMap', greenMap', blueMap'];
  47.         catch err
  48.                 display(repmat('@',1,7))
  49.                 disp(getReport(err,'extended','hyperlinks','on'));
  50.                 display(repmat('@',1,7))
  51.         end
download getOptions.m

Matlab M
  1. function [options] = getOptions(options,inputArgs,varargin)
  2.     % gets default options for a function, replaces with inputArgs inputs if they are present
  3.     % biafra ahanonu
  4.     % 2014.02.17 [22:21:49]
  5.     %
  6.     % inputs
  7.     %   options - structure with options as fieldnames
  8.     %   inputArgs - varargin containing name-value pairs passed from parent function.
  9.  
  10.     % list of valid options to accept, simple way to deal with illegal user input
  11.     validOptions = fieldnames(options);
  12.  
  13.     % loop over each input name-value pair, check whether name is valid and overwrite fieldname in options structure.
  14.     for i = 1:2:length(inputArgs)
  15.         val = inputArgs{i};
  16.         if ischar(val)
  17.             % allow input of an options structure that overwrites existing fieldnames with its own, for increased flexibility
  18.             if strcmp('options',val)
  19.                 inputOptions = inputArgs{i+1};
  20.                 [options] = mirrorRightStruct(inputOptions,options);
  21.             elseif ~isempty(strmatch(val,validOptions))
  22.                 options.(val) = inputArgs{i+1};
  23.             end
  24.         else
  25.             continue;
  26.         end
  27.     end
  28.  
  29. function [pullStruct] = mirrorRightStruct(pushStruct,pullStruct)
  30.     % overwrites fields in pullStruct with those in pushStruct, other pullStruct fields rename intact
  31.     % more generally, copies fields in pushStruct into pullStruct, if there is an overlap in field names, pushStruct overwrites.
  32.     pushNames = fieldnames(pushStruct);
  33.     for name = 1:length(pushNames)
  34.         iName = pushNames{name};
  35.         pullStruct.(iName) = pushStruct.(iName);
  36.     end

-biafra
bahanonu [at] alum.mit.edu

more articles to enjoy:

quicklinks github
12 august 2012 | programming

quicklinks is a new homepage for those looking for efficiency over lavish use of big buttons commonly seen in Firefox, Opera and other brow[...]ser's homepages. As quicklinks is still a little rough around the edges and needs to be updated, I've added it to GitHub to allow me to update it easier.

sochi olympic stats: medal count
19 february 2014 | america

There have been several articles that re-order the medal count ranking by comparing medals to population or GDP, which then show small coun[...]tries topping the charts. This analysis ignores some obvious facts: small countries are over-represented in the number of athletes they send and the relationship between athletes sent and medals is linear. I present a brief analysis to support and expand on these claims.

expanding markets and selling sex
07 january 2012 | essay

Sex sells. That simple statement has been used throughout the ages to justify lewd advertisements and other forms of communication, wh[...]ether they are through pictures, videos or other mediums. But while other industries, from cigarette companies to Hollywood, use it passingly to entice people into considering a product they otherwise would not, there is an industry and culture which bases its existence upon this simple fact. That industry is pornography.

A provocative essay I wrote in high school, and touched up for presentation here, that take a look at the porn industry: what makes it tick, how it has innovated and the people involved.

stanford bing concert hall: first impressions
15 december 2012 | stanford

Designed by architect Richard Olcott (Ennead Architects) and sound designer Dr. Yasuhisa T[...]oyota (Nagata Acoustics), the Bing Concert Hall is stunning. Robert Campbell (Fisher Dachs Associates) was on hand during the second sound check (along with Richard and Dr. Toyota) to discuss the philosophy behind the building, a bit of history, and where they hope it will be in the future. This post is my impressions of the place along with notes from their interview.

©2006-2017 | biafra ahanonu | updated 12 december 2017
biafra ahanonu