If I open a previously (before R2014b
) saved figure, the colors I used, for instance r
, k
, ... would appear according to the colormap they have been saved with. What is the fast way to convert the colors to their equivalent colors in the new colormap parula.
By equivalent colors I mean the standard sequence of colors that MATLAB
utilizes when we usehold on
command after each plot
command, without setting the color property in the `plot'. something like this:
plot(x,y1);hold on;plot(x,y2);
It should be pretty much automatic If I change the default colormap of the plot, but it is not. Is there a command for that?
The plots I have, each includes more than 20 curves that makes it annoying to change the colors manually.
The following seems to work.
open example_figure.fig %// open old file in R2014b
ax = gca;
ch = ax.Children; %// the children are lines or other objects
co = ax.ColorOrder; %// this is the new set of colors
M = size(co,1);
p = 1; %// index of new color
for n = numel(ch):-1:1 %// start with lines plotted earlier. These have higher index
if strcmp(ch(n).Type,'line') %// we are only interested in lines
ch(n).Color = co(mod(p-1,M)+1,:); %// cycle through new colors
p = p + 1; %// increase color index
end
end
The key is that, as stated in Loren's blog,
The line colors used in plots are controlled by the
ColorOrder
property of theAxes
object.
This is the property that stores the new hold on
-colors used in R2014b. But this property applies to newly plotted lines, not to those already present from the file. So what my code above does is apply the colors defined by ColorOrder
to the Children
of the axes that are of type 'line'
.
I've observed (at least in R2010b) that newer plots have lower indices within the children
array. That is, when a new plot is added to the axes, it gets the first position in the Children
array, pushing older plots to higher indices. That's why in the for
loop above the children index (n
) is descending while the new-color index (p
) is ascending. This assures that the line that was plotted first (higher index) gets the first of the new colors, etc.
As an example, let's create a figure in R2010b:
plot(1:3, 'b')
hold on
plot(4:6, 'r')
plot(7:9, 'g')
The transformed figure is