# How to get the rgb code from Matlab figure

18 views (last 30 days)
Khalid Ibne Masood on 29 Apr 2020
Edited: Adam Danz on 29 Apr 2020
Hello Everyone,
I have attached a matlab figure here. This plot was generated from Matlab and I need to know the RGB color code for each color used in this picture. I know that there are exactly 5 colors have been used for this plot, I need to extract these 5 colors. Please help me to solve this problem.
Thank you.
Khalid

Adam Danz on 29 Apr 2020
Do you have the fig file (if so, attach it) and do you know which function was used to generate the plot?
Khalid Ibne Masood on 29 Apr 2020
Thank you, here I have attached the .fig file.
I believe 'surf' was used to generate the plot.
Thank you.
Khalid

Adam Danz on 29 Apr 2020
Edited: Adam Danz on 29 Apr 2020
Retreiving the colors from a surface plot can be trickly due to the interaction between the color properties of a surface object. If you plan to apply this solution to a variety of surface plots with varying color properties, you'll need to carefully read about the color properties and how they interact with eachother: https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.surface-properties.html#d120e1097631
% Open the figure
fig = openfig('Question.fig');
% Find the axis handle
ax = findobj(fig, 'Type', 'axes');
% Find the surface handle
surfObj = ax.Children;
% Get the colormap
cmap = ax.Colormap;
% scale the color data values to the range of the
% colormap to match the color data with the colormap rows.
cval = round((surfObj.CData - ax.CLim(1))./range(ax.CLim) .* (size(cmap,1)-1)) +1;
% List the unique colors in order from left to right.
uniqueRGB = cmap(unique(cval(:),'stable'),:);
Results:
uniqueRGB =
0.5 0 0
0.9375 1 0.0625
0.5625 1 0.4375
0.4375 1 0.5625
0 0 0.5625
Plot a large dot above each color section to check the results
% Add dots to plot to confirm color
hold on
h = scatter3([.1 .35 .6 .80 .95], repmat(1,1,size(uniqueRGB,1)), repmat(max(ax.ZLim),1,size(uniqueRGB,1)), ...
200, uniqueRGB, 'filled', 'MarkerEdgeColor', 'k', 'LineWidth', 2); 