MATLAB: Want to generate RGB vertical color bar signal

Image Processing Toolboxrgb barrgb vertical color bar signalvertical color bar

Hello there, I am Hossain. As part of my class assignment I have to generate a RGB vertical color bar as shown in the picture.
I have tried to make myself. What have I done is given bellow:
y= [100 100 100 100 100 100 100 100; 0 0 0 0 0 0 0 0];
b = bar(y,1);
b(1).FaceColor = [1 1 1];
b(2).FaceColor = [1 1 0];
b(3).FaceColor = [0 1 1];
b(4).FaceColor = [0 1 0];
b(5).FaceColor = [1 0 1];
b(6).FaceColor = [1 0 0];
b(7).FaceColor = [0 0 1];
b(8).FaceColor = [0 0 0];
And the corresponding graph i have generated is :
But I want to generate the image as the first image. Would you please help me to generate the image as the first one?. my knowledge on Matlab is not too much deep.
Thanks in advance.

Best Answer

Try this:
% First, create just one single line.
barWidth = 100; % Width of one bar in pixels.
oneLine = [zeros(1, barWidth), ones(1, barWidth), 2 * ones(1, barWidth), 3 * ones(1, barWidth), 4 * ones(1, barWidth), 5 * ones(1, barWidth), 6 * ones(1, barWidth), 7 * ones(1, barWidth)];
% Replicate vertically
heightInPixels = 8 * barWidth; % Whatever you want.
indexedImage = repmat(uint8(oneLine), [heightInPixels, 1]);
% Display the image
imshow(indexedImage);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
uiwait(helpdlg('Now we will apply a custom colormap.'));
% Apply a custom colormap.
customColorMap = [1 1 1
1 1 0;
0 1 1;
0 1 0;
1 0 1;
1 0 0;
0 0 1;
0 0 0];
colormap(customColorMap);
caxis([0, 7]);
colorbar;
axis on;
Then use text(x, y, 'HDM STGT') if you want that text over the image.
Use
rgbImage = ind2rgb(indexedImage, customColorMap);
if you want to create an RGB image.