Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
creating new colormaps as function handles

Subject: creating new colormaps as function handles

From: Kirk

Date: 19 Aug, 2011 15:47:09

Message: 1 of 17

I need to use a non-predifined colormap within a function (confplot_t from the file exchange). The function uses a file handle to the colormap argument such that the colormap "cool" would be called with "@cool".

However, when I try and create a new colormap, the colormap is in my workspace, and I get an error that the new colormap argument must be a function handle.

Is there a way to create a new colormap, and add that colormap to MATLAB's predefined list of colormaps so that it can be called with a function handle such as... "@new_cool"?

Thank you

Subject: creating new colormaps as function handles

From: Bruno Luong

Date: 19 Aug, 2011 16:34:09

Message: 2 of 17

"Kirk" <kwythers.nospam@umn.edu> wrote in message <j2m0ht$b0f$1@newscl01ah.mathworks.com>...
> I need to use a non-predifined colormap within a function (confplot_t from the file exchange). The function uses a file handle to the colormap argument such that the colormap "cool" would be called with "@cool".
>
> However, when I try and create a new colormap, the colormap is in my workspace, and I get an error that the new colormap argument must be a function handle.
>
> Is there a way to create a new colormap, and add that colormap to MATLAB's predefined list of colormaps so that it can be called with a function handle such as... "@new_cool"?
>
> Thank you

If you want to overshadow Matlab file colormap(), rename the original colormap.m to
matlabcolormap.m (type <which colormap> to see where it is located), then use this function:

function varargout = colormap(varargin)

if nargin >= 1
    map = varargin{end};
    if isa(map,'function_handle')
        varargin{end} = feval(map);
    end
end
[varargout{1:nargout}] = matlabcolormap(varargin{:});

end % colormap

WARNING: use at your own risk.

Bruno

Subject: creating new colormaps as function handles

From: Kirk

Date: 19 Aug, 2011 16:57:09

Message: 3 of 17

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j2m3a1$k6h$1@newscl01ah.mathworks.com>...
> "Kirk" <kwythers.nospam@umn.edu> wrote in message <j2m0ht$b0f$1@newscl01ah.mathworks.com>...
> > I need to use a non-predifined colormap within a function (confplot_t from the file exchange). The function uses a file handle to the colormap argument such that the colormap "cool" would be called with "@cool".
> >
> > However, when I try and create a new colormap, the colormap is in my workspace, and I get an error that the new colormap argument must be a function handle.
> >
> > Is there a way to create a new colormap, and add that colormap to MATLAB's predefined list of colormaps so that it can be called with a function handle such as... "@new_cool"?
> >
> > Thank you
>
> If you want to overshadow Matlab file colormap(), rename the original colormap.m to
> matlabcolormap.m (type <which colormap> to see where it is located), then use this function:
>
> function varargout = colormap(varargin)
>
> if nargin >= 1
> map = varargin{end};
> if isa(map,'function_handle')
> varargin{end} = feval(map);
> end
> end
> [varargout{1:nargout}] = matlabcolormap(varargin{:});
>
> end % colormap
>
> WARNING: use at your own risk.

If I am reading this correctly, the original colormap "colormap" is preserved, but the function_handle is assigned to the new "matlabcolormap". Correct?

In my case I have a collection of new colors (collected in the file exchange function "othercolor". Each 50 or so colormaps are held in the the file "colorData.mat"). So for your suggestion, I could try renaming colormap.m to colorData.mat?

Or am I missing your point?

Subject: creating new colormaps as function handles

From: Bruno Luong

Date: 19 Aug, 2011 17:13:09

Message: 4 of 17

"Kirk" <kwythers.nospam@umn.edu> wrote in message <j2m4l5$oke$1@newscl01ah.mathworks.com>...

>
> If I am reading this correctly, the original colormap "colormap" is preserved, but the function_handle is assigned to the new "matlabcolormap". Correct?
>
> In my case I have a collection of new colors (collected in the file exchange function "othercolor". Each 50 or so colormaps are held in the the file "colorData.mat"). So for your suggestion, I could try renaming colormap.m to colorData.mat?
>
> Or am I missing your point?

Oh actually I missed your point. You don't want to overshadow the function COLORMAP with function handles input do you? [ I think the answer is no ].

If you have a colormap (2d array), e.g., ColorMapNumber10 and want a function handle to return it just do

AllColorMaps = load('colorData.mat);
ColorMapNumber10 = ... % put here a command to retrieve the Colormap #10

ColorMapNumber10Fun = @() ColorMapNumber10;

Use ColorMapNumber10Fun as input of the function in the package that requires it.

Bruno

Subject: creating new colormaps as function handles

From: Steven_Lord

Date: 19 Aug, 2011 17:25:32

Message: 5 of 17



"Kirk" <kwythers.nospam@umn.edu> wrote in message
news:j2m0ht$b0f$1@newscl01ah.mathworks.com...
> I need to use a non-predifined colormap within a function (confplot_t from
> the file exchange). The function uses a file handle to the colormap
> argument such that the colormap "cool" would be called with "@cool".
> However, when I try and create a new colormap, the colormap is in my
> workspace, and I get an error that the new colormap argument must be a
> function handle.
>
> Is there a way to create a new colormap, and add that colormap to MATLAB's
> predefined list of colormaps so that it can be called with a function
> handle such as... "@new_cool"?

Sure, just create a function file and have that function file return the
desired colormap. How you generate that colormap matrix inside your function
doesn't really matter to MATLAB. Hard-coding the values; constructing it
using references to functions like EYE, RAND, GALLERY, etc; LOADing from a
MAT-file; even asking the user to enter the colormap manually using INPUT:
any of these would work.

If you need a quick solution just for this particular session, and the
colormap can be generated by one expression, you could create an anonymous
function.

randomColormap = @() rand(25, 3);

or, if you want to use the same random matrix each time:

x = rand(25, 3);
xAsColormap = @() x;

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: creating new colormaps as function handles

From: Kirk

Date: 19 Aug, 2011 20:28:11

Message: 6 of 17

"Steven_Lord" <slord@mathworks.com> wrote in message <j2m6ac$112$1@newscl01ah.mathworks.com>...
>
>
> "Kirk" <kwythers.nospam@umn.edu> wrote in message
> news:j2m0ht$b0f$1@newscl01ah.mathworks.com...
> > I need to use a non-predifined colormap within a function (confplot_t from
> > the file exchange). The function uses a file handle to the colormap
> > argument such that the colormap "cool" would be called with "@cool".
> > However, when I try and create a new colormap, the colormap is in my
> > workspace, and I get an error that the new colormap argument must be a
> > function handle.
> >
> > Is there a way to create a new colormap, and add that colormap to MATLAB's
> > predefined list of colormaps so that it can be called with a function
> > handle such as... "@new_cool"?
>
> Sure, just create a function file and have that function file return the
> desired colormap. How you generate that colormap matrix inside your function
> doesn't really matter to MATLAB. Hard-coding the values; constructing it
> using references to functions like EYE, RAND, GALLERY, etc; LOADing from a
> MAT-file; even asking the user to enter the colormap manually using INPUT:
> any of these would work.
>
> If you need a quick solution just for this particular session, and the
> colormap can be generated by one expression, you could create an anonymous
> function.
>
> randomColormap = @() rand(25, 3);
>
> or, if you want to use the same random matrix each time:
>
> x = rand(25, 3);
> xAsColormap = @() x;


Thanks for the reply.

So if I have a set of colormaps in a .mat file named "colorData" (there are 402 color maps in "colorData" with names like "Blue3", can I load all of the colormaps with something like :

newColormap = @() colorData(:,:);

or would I have to load each colormap separately with something like:

newColormap = @() colorData('Blue3');

Subject: creating new colormaps as function handles

From: Kirk

Date: 19 Aug, 2011 20:46:09

Message: 7 of 17

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j2m5j5$rpi$1@newscl01ah.mathworks.com>...
> "Kirk" <kwythers.nospam@umn.edu> wrote in message <j2m4l5$oke$1@newscl01ah.mathworks.com>...
>
> >
> > If I am reading this correctly, the original colormap "colormap" is preserved, but the function_handle is assigned to the new "matlabcolormap". Correct?
> >
> > In my case I have a collection of new colors (collected in the file exchange function "othercolor". Each 50 or so colormaps are held in the the file "colorData.mat"). So for your suggestion, I could try renaming colormap.m to colorData.mat?
> >
> > Or am I missing your point?
>
> Oh actually I missed your point. You don't want to overshadow the function COLORMAP with function handles input do you? [ I think the answer is no ].
>
> If you have a colormap (2d array), e.g., ColorMapNumber10 and want a function handle to return it just do
>
> AllColorMaps = load('colorData.mat);
> ColorMapNumber10 = ... % put here a command to retrieve the Colormap #10
>
> ColorMapNumber10Fun = @() ColorMapNumber10;
>
> Use ColorMapNumber10Fun as input of the function in the package that requires it.


Bruno,

Thanks for the reply,

I tired:

>> allColorMaps = load('colorData.mat');
>> Blues3 = allColorMaps('Blues3');
>> Blues3 = allColorMaps.Blues3;
>> Blues3Fun = @() Blues3;
>> confplot_t(x,y_mean,y_std,100,[0.01:0.01:0.5],@Blues3Fun,1);

But got the error:

??? Error: "Blues3Fun" was previously used as a variable,
 conflicting with its use here as the name of a function or command.
 See MATLAB Programming, "How MATLAB Recognizes Function Calls That Use
 Command Syntax" for details.

btw... this comand with the function "confplot_t" works fine with @copper


Any thoughts?

Subject: creating new colormaps as function handles

From: Bruno Luong

Date: 19 Aug, 2011 20:47:10

Message: 8 of 17

"Kirk" <kwythers.nospam@umn.edu> wrote in message <j2mh0r$82k$1@newscl01ah.mathworks.com>...

> So if I have a set of colormaps in a .mat file named "colorData" (there are 402 color maps in "colorData" with names like "Blue3", can I load all of the colormaps with something like :
>
> newColormap = @() colorData(:,:);
>
> or would I have to load each colormap separately with something like:
>
> newColormap = @() colorData('Blue3');

It's all how the colormaps are stored in colorData.mat. How do you access them normally?

Just put the command in the anonymous function that return the colormap as array of 2d.

% For example if after loading

AllColormap = load('colorData.mat');

% the Blue3 colormap can be access by,

AllColormap.Blue3 % <- command *you* should know, not us.

% then declare

% a Function @ that accept no argument () and returns the map AllColormap.Blue3
Blue3Fun = @() AllColormap.Blue3;

You should try to understand Matlab syntax we show in this thread, rather than seeking for ready-to-chew list of commands without look for what do they mean.

Bruno

Subject: creating new colormaps as function handles

From: Bruno Luong

Date: 19 Aug, 2011 20:58:09

Message: 9 of 17

"Kirk" <kwythers.nospam@umn.edu> wrote in message <j2mi2h$bdk$1@newscl01ah.mathworks.com>...

> >> allColorMaps = load('colorData.mat');
> >> Blues3 = allColorMaps('Blues3');
> >> Blues3 = allColorMaps.Blues3;
> >> Blues3Fun = @() Blues3;
> >> confplot_t(x,y_mean,y_std,100,[0.01:0.01:0.5],@Blues3Fun,1);
>

Correct it to:

confplot_t(x,y_mean,y_std,100,[0.01:0.01:0.5],Blues3Fun,1);

"@Blues3Fun" stands for a function handles defined in mfile with the name Blues3Fun.m

"Blues3Fun = @() Blues3" stands for put an anonymous variable @() Blue3 in the variable named Blues3Fun. So Blues3Fun is already a function handle.

Bruno

Subject: creating new colormaps as function handles

From: Kirk

Date: 23 Aug, 2011 19:19:27

Message: 10 of 17

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j2mip1$dl5$1@newscl01ah.mathworks.com>...
> "Kirk" <kwythers.nospam@umn.edu> wrote in message <j2mi2h$bdk$1@newscl01ah.mathworks.com>...
>
> > >> allColorMaps = load('colorData.mat');
> > >> Blues3 = allColorMaps('Blues3');
> > >> Blues3 = allColorMaps.Blues3;
> > >> Blues3Fun = @() Blues3;
> > >> confplot_t(x,y_mean,y_std,100,[0.01:0.01:0.5],@Blues3Fun,1);
> >
>
> Correct it to:
>
> confplot_t(x,y_mean,y_std,100,[0.01:0.01:0.5],Blues3Fun,1);
>
> "@Blues3Fun" stands for a function handles defined in mfile with the name Blues3Fun.m
>
> "Blues3Fun = @() Blues3" stands for put an anonymous variable @() Blue3 in the variable named Blues3Fun. So Blues3Fun is already a function handle.

Thank you for the suggestion. However, leaving off the @ throughs the error:

??? Error using ==> feval
Argument must contain a string or function_handle.

Error in ==> confplot_t at 76
G = feval(CM,nP+2).^gamma;

It seems that the function "confplot_t" requires a function handle for the CM (colormap) argument. And furthermore will not accept a function handle that contains a another function, such as "@othercolor('Blues3')".

Taking a different tact, today I tried modifying one of the default colormaps in

~/Applications/Matlab/toolbox/matlab/graph3d/

I started with the file "winter.m". Which looks like this:

function c = winter(m)
%WINTER Shades of blue and green color map
% WINTER(M) returns an M-by-3 matrix containing a "winter" colormap.
% WINTER, by itself, is the same length as the current figure's
% colormap. If no figure exists, MATLAB creates one.
%
% For example, to reset the colormap of the current figure:
%
% colormap(winter)
%
% See also HSV, GRAY, HOT, BONE, COPPER, PINK, FLAG,
% COLORMAP, RGBPLOT.

% Copyright 1984-2004 The MathWorks, Inc.
% $Revision: 1.8.4.2 $ $Date: 2005/06/21 19:31:52 $

if nargin < 1, m = size(get(gcf,'colormap'),1); end
r = (0:m-1)'/max(m-1,1);
c = [zeros(m,1) r .5+(1-r)/2];

And changed it to this (naming the file "Blues3":

function c = Blues3

c = othercolor('Blues3');

Now the command "Blues3" returns a 64x3 matrix of colormap values, and the command "@Blues3" returns the value @Blues3, just like the corresponding commands "winter: and "@winter". However placing @Blues3 as the CM (colormap) argument in the function confplot_t gives the error:

>> confplot_t(x,dl_aspn_fixed_npp_1xDCO2_x_s(:,1),dl_aspn_fixed_npp_1xDCO2_x_s(:,2),100,[0.01:0.01:0.5],@Blues3,1);
??? Error using ==> Blues3
Too many input arguments.

Error in ==> confplot_t at 76
G = feval(CM,nP+2).^gamma;

While using a handle to the default colormap winter,
>> confplot_t(x,dl_aspn_fixed_npp_1xDCO2_x_s(:,1),dl_aspn_fixed_npp_1xDCO2_x_s(:,2),100,[0.01:0.01:0.5],@winter,1);

draws the figure perfectly.

This result just does not seem logical to me. If solo command line calls to "Blues3" produce the same result as "winter" and "@Blues3" produces the same result as "@winter", then what am I missing here?

This bit of code in the function "confplot_t" that seems to take the colormap argument and require a @ the fevel command here:

% Prepare the color map
G = feval(CM,nP+2).^gamma;
G = G(2:nP+1,:);

Subject: creating new colormaps as function handles

From: Bruno Luong

Date: 23 Aug, 2011 19:42:12

Message: 11 of 17

"Kirk" <kwythers.nospam@umn.edu> wrote in message <j30ufv$esb$1@newscl01ah.mathworks.com>...

> Thank you for the suggestion. However, leaving off the @ throughs the error:
>
> ??? Error using ==> feval
> Argument must contain a string or function_handle.
>
> Error in ==> confplot_t at 76
> G = feval(CM,nP+2).^gamma;

Obviously the author of confplot_t expect a function with 1 input argument, which is the number rows of the output color map (see doc JET or GRAY for examples).

You have customized color maps reading from file, but they are fixed size. So I don't know how you intend to deal with the varying size of the map the author expect the function to return.

It just doesn't seem to fit together.

Bruno

Subject: creating new colormaps as function handles

From: Bruno Luong

Date: 23 Aug, 2011 19:48:26

Message: 12 of 17

You might try to define
 
function c = Blues3(varargin)
c = othercolor('Blues3');

so Blue3 will ignore the required number of colors in the map, and returns anyway a fix size map.

The risk is it might create inconsistency with the calling code would expect to receive.

Bruno

Subject: creating new colormaps as function handles

From: Bruno Luong

Date: 23 Aug, 2011 19:55:27

Message: 13 of 17

Or better still, just interpolate the original colormap and extend it to the right size

function c = Blues3(n)
c = othercolor('Blues3');
if nargin>=1
   c = interp1(linspace(0,1,size(c,1)),c,linspace(0,1,n));
end
end

% Bruno

Subject: creating new colormaps as function handles

From: Kirk

Date: 23 Aug, 2011 20:03:27

Message: 14 of 17

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j30vqk$jhp$1@newscl01ah.mathworks.com>...
> "Kirk" <kwythers.nospam@umn.edu> wrote in message <j30ufv$esb$1@newscl01ah.mathworks.com>...
>
> > Thank you for the suggestion. However, leaving off the @ throughs the error:
> >
> > ??? Error using ==> feval
> > Argument must contain a string or function_handle.
> >
> > Error in ==> confplot_t at 76
> > G = feval(CM,nP+2).^gamma;
>
> Obviously the author of confplot_t expect a function with 1 input argument, which is the number rows of the output color map (see doc JET or GRAY for examples).
>
> You have customized color maps reading from file, but they are fixed size. So I don't know how you intend to deal with the varying size of the map the author expect the function to return.
>
> It just doesn't seem to fit together.


the default colormaps jet and gray also produce 64x3 matrix of RGB values. It seems to me that my solution of creating a function which calls the extended colormap collection "othercolor" with a single argument should work. I'll give you that it is not very elegant because you would have to create a function for each of the 400 and some different color maps included in "othercolor", but... the command "@Blues3" should satisfy the requirement for a single argument function handle, which when called, produces the same format 64x3 colormap matrix as autumn, or jet, or gray produces.

Sorry to be pain on this one, but it just doesn't make sense why the single argument "@Blues3" which calls the two argument function "othercolor('Blues3') should not work.

Are you saying that since the function "Blues3" contains a line with two arguments, that it is those two arguments that are messing up the function confplot_t?

Subject: creating new colormaps as function handles

From: Kirk

Date: 23 Aug, 2011 20:07:07

Message: 15 of 17

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j3106a$kql$1@newscl01ah.mathworks.com>...
> You might try to define
>
> function c = Blues3(varargin)
> c = othercolor('Blues3');



Worked!!!!

Subject: creating new colormaps as function handles

From: Bruno Luong

Date: 23 Aug, 2011 20:10:49

Message: 16 of 17


>
> Sorry to be pain on this one, but it just doesn't make sense why the single argument "@Blues3" which calls the two argument function "othercolor('Blues3') should not work.

 I never say the problem is at the line othercolor('Blues3). Did I?

>
> Are you saying that since the function "Blues3" contains a line with two arguments, that it is those two arguments that are messing up the function confplot_t?

If you want to understand *read*

help FEVAL
help JET
help WINTER

Sorry I don't have patient to repeat the same explanation over and over.

Bruno

Subject: creating new colormaps as function handles

From: velan

Date: 22 Jan, 2012 19:36:30

Message: 17 of 17

I need simulink model file for digital controlled oscillator, if any one having pls send to me...

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us