MATLAB: How to put a title on a colorbar

colorbar title surf titlestring

I have a 3D surface surf(X,Y,Z) viewed from view(0,90) with a colorbar which I want to put a title on. The help instructions talk about an lcolorbar, TitleString and ZlabelString but there's no example and I'm lost.
[X Y]=meshgrid(0:100,0:100);
Z=Y;
surf(X,Y,Z);
view(0,90);
hcb=colorbar;
?????? what next to put a title on the colorbar please ?????
Maybe something like set(get(hcb,'Title'),'cb title') but I wouldn't be asking if that worked …
Thanks.

Best Answer

Using the handle for the colorbar (in your case, the variable hcb), you can locate the colorbar handle title using the get function. Once you've found the handle for the colorbar title, you can directly change the title string via the set function. When working with figures in MATLAB, you'll often find yourself referencing graphic handles, so I recommend brushing up on them!
In your case, you can change the colormap title with just a few lines of code! Here is an example, which you can add after your example code above, to get you started:
colorTitleHandle = get(hcb,'Title');
titleString = 'A title';
set(colorTitleHandle ,'String',titleString);
Hope this clarifies things a bit!