Can i get 12 different colors for 12 different variables in matlab? So far I could only get 6 colors because i don't know the values for the other colors:

79 views (last 30 days)
if(strcmp(matrix(i,2),"C1")==1)
redundant sRGB matrix values replaced with ...
end
fs = 10;
score = repmat(linspace(1,6,12).',[1 3]);
matrix = ["" "C1"; "" "C2"; "" "C3"; "" "C4"; "" "C5"; "" "C6"; ...
"" "C7"; "" "C8"; "" "C9"; "" "C10"; "" "C11"; "" "C12"];
lb = strcat(repmat("Test string ",[12 1]),matrix(:,2));
%I am trying to classify each C by a different color but I only know 6 color numbers without adding decimals. Is there a way to
%add
%12 different colors in this format? i.e. [1.5, val = 0, 1.2]

Accepted Answer

DGM
DGM on 28 Feb 2023
Edited: DGM on 1 Mar 2023
I'm not really sure what you're asking for or why. The thing you have is a set of repeated pairs. The thing you're asking for [1.5 0 1.2] isn't a valid unit-scale color tuple. Is val always zero? If not, will it also exceed [0 1]? Is there some particular meaning to the sequence and scale of the colors? If so, we'd obviously need to know.
If you just need 12 unique colors, you can use one of the colormap generator functions like jet(), parula(), etc to generate a map of a specified length. If you want to group them in pairs, you can use something like the attached function.
The rest of this can be cleaned up to eliminate the repeated code. Normally, you can use a switch-case structure instead of repeated if-else tests to do this, but in this case, the conditionally-executed code is identical except for one parameter.
% some placeholder inputs
fs = 10;
score = repmat(linspace(1,6,12).',[1 3]);
matrix = ["" "C1"; "" "C2"; "" "C3"; "" "C4"; "" "C5"; "" "C6"; ...
"" "C7"; "" "C8"; "" "C9"; "" "C10"; "" "C11"; "" "C12"];
lb = strcat(repmat("Test string ",[12 1]),matrix(:,2));
% generate a color table of appropriate length
colortable = tab20(12);
% demonstrate
xlim([0 10])
ylim([0 7])
hold on
for i = 1:12
% just get the trailing numeric part
thisclass = str2double(strrep(matrix(i,2),'C',''));
% print the text
text(score(i,1),score(i,2),score(i,3),lb(i), ...
'Color',colortable(thisclass,:),'fontsize',fs,'FontWeight','bold');
end
The attached function is from here. There are many other similar tools on the File Exchange.
  9 Comments
Stephen23
Stephen23 on 2 Mar 2023
@Chanille: i highly recommend watching this video about the developing the new MatPlotLib colormaps:
It is an compact, entertaining, yet very educational introduction to the world of color perception modelling.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!