Passing no of colours in colormap as a variable

2 views (last 30 days)
Hi,
I am plotting several different contour plots in a code. The code for plotting them is below. My problem is that for publication purpose, I want to use 3500 colours instead of 500 (as shown below) since the figure then looks smooth. But obviously Matlab runs extremely slow on my Mac. I wanted to pass no. of colours as a variable and change it at one place. So that I don't need to change it at several places in the code. When I pass the variable e.g. noColors = 3500, Matlab complains at colormap jet() line saying it needs finite values. How to play around this problem?
contourf( a0Grid, omegaPGrid, log10(freqShift), 500, 'Edgecolor', 'none' )
colormap jet(500)
  4 Comments
Image Analyst
Image Analyst on 19 Feb 2013
But colormap(jet(noColors)) does? I didn't know that there would be a difference between the function call method and the command line method of calling colormap. It would be interesting to know the reason for that. I know there is a difference for some functions, like those that take a filename such as load() or save().
Walter Roberson
Walter Roberson on 19 Feb 2013
colormap jet(500)
is equivalent to
colormap('jet(500)')
which is not a documented facility. It happens to work because colormap() feval()'s its argument when its argument is a string, but the documentation doesn't say that. The one example uses the function form equivalent to
colormap(jet(500))
which calls jet(500) and returns that numeric array as the argument to colormap()

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Feb 2013
Try
colormap(jet(noColors))
  5 Comments
Walter Roberson
Walter Roberson on 20 Feb 2013
When you have
A B(C)
then due to MATLAB's "command / function equivalence", what would be passed to the function "A" would be the string 'B(C)'. "A" would then be responsible for interpreting the string. It happens that colormap() processes received strings using feval() or eval() and so runs whatever code is there, but that is more of an exception.
For example,
cd hotpink(128)
is not going to call hotpink(128): instead it would try to cd to a directory literally named 'hotpink(128)' complete with the () in the name.
When you use
A(B(C))
instead, then B(C) is evaluated as an expression, and the result (whatever data type it happens to be) is passed into A.
Naveen
Naveen on 20 Feb 2013
Thanks for the explanation! You're a great help here!

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!