Passing no of colours in colormap as a variable

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

colormap jet(500) works fine for me.
Sadly not always for me.
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().
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

Try
colormap(jet(noColors))

5 Comments

Thanks! Is there also any way to pass the no. of DPIs for printing the figure as variables?
print( ...., sprintf('-r%d', Resolution), ....)
Thanks again! It's really interesting that just by using parenthesis () one has different behaviour in Matlab. I was using print command before without () parenthesis and your solution doesn't work that way, but it works great with parenthesis! Is there some inherent functionality like feval that gets invoked every time you use () parenthesis as you try to suggest in your previous answer to Image Analyst?
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.
Thanks for the explanation! You're a great help here!

Sign in to comment.

More Answers (0)

Products

Tags

Community Treasure Hunt

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

Start Hunting!