MATLAB: Colorbar not matching colormap

colorcolorbarcolormapjetMATLABscatter plot

I want to do a scatter plot with points in different colors in the following way (Using Matlab 2018b).
colormap = jet(256);
v = rescale(pwdb, 1, 256);
numValues = length(pwdb);
markerColors = zeros(numValues, 3);
figure
for k = 1 : numValues
row = round(v(k));
markerColors(k, :) = colormap(row, :);
end
scatter(location(1,:), location(2,:), [], markerColors,'filled');
This works fine, the points are plotted with the right colors.
However, when I try to add the colorbar (with ), I get the following:
colorbar
What is the way to include the color red in the bar and the right values?
Thanks

Best Answer

colormap is a function and you just blew it away with this line of code:
colormap = jet(256);
That overwrote the colormap function (luckily not permanently) with a 256-by-3 double array.
Try it this way
colormap(jet(256));