MATLAB Answers

0

How to specify line color using a hexadecimal color code

Asked by XAXRXTX on 23 Apr 2019
Latest activity Answered by Akira Agata
on 24 Apr 2019
Hi,
why the plot function return an error message when I specify the color as a hexadecimal color code i.e
x = linspace(-2*pi,2*pi);
y = sin(x);
plot( x , y , 'Color' , '#FF0000' )
Error using plot
Specified character vector is an invalid color value.

  0 Comments

Sign in to comment.

1 Answer

Answer by Akira Agata
on 24 Apr 2019
 Accepted Answer

It would be due to your MATLAB version. Hexadecimal color code can be used in the latest version (R2019a).
If your version is R2018b or older, you have to convert hex color code to 1-by-3 RGB array, like:
x = linspace(-2*pi,2*pi);
y = sin(x);
% Convert color code to 1-by-3 RGB array (0~1 each)
str = '#FF0000';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
figure
plot(x, y, 'Color', color)

  0 Comments

Sign in to comment.