1

I was trying to invert the colormap (change positive/negative color directions) with a call to 'flipud':

colormap(flipud(hot)); % flips color map
colormap(flipud('hot')); % no effect

I didn't expect this result. For clarification, is this because the inner call to flipud is flipping the string (resulting in an identical string) and loading the same colormap it was before? While without the quotes, it is flipping the actual matrix of color values?

1 Answers1

2

hot is a function that returns a colormap matrix:

hot()

ans =

    0.0416666666666667  0  0
    0.0833333333333333  0  0
                 0.125  0  0

So in the first case you are correctly flipping a matrix. In the second case, as you correctly stated, you are attempting to flip the character array 'hot' and build a colormap out of it.

The fundamental reason beyond this behavior is that functions without arguments, in Matlab, can be called without using parentheses. Do you think pi is a numerical constant? Wrong, pi is a built-in function. Try this out in your console:

pi

ans =

      3.14159265358979

pi()

ans =

      3.14159265358979

An alternative, although I don't see the point of using it unless you are working with user input or something like along these lines, would be writing your call as follows:

text = 'hot';
colormap(flipud(eval(text)));
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • I've noticed that the colormap function can accept string inputs like colormap('redblue') after downloading [link](https://www.mathworks.com/matlabcentral/fileexchange/25536-red-blue-colormap). Is colormap(string) a documented behavior? (context on how I originally encountered the string flip). Also works for builtin colormaps like 'hot' – Peter Barrett Bryan Jan 06 '18 at 23:29
  • Done! Had to wait for the time delay on accepting an answer to pass. Many thanks, again! The string argument does seem to be preserved for some other values, though. I don't see the option in the documentation. Try colormap(flipud('hot')); – Peter Barrett Bryan Jan 06 '18 at 23:37
  • Internally, a `feval` is being performed so if the function that returns the colormap matrix is not defined, the function throws an error. But this cannot let you flip it. – Tommaso Belluzzo Jan 06 '18 at 23:42