MATLAB Answers


How to change color from a .fig file having subplots

Asked by Emanuele Rondanina on 31 Aug 2017
Latest activity Edited by Adam
on 31 Aug 2017
I'm creating a function which takes as input a .fig file and it modifies its properties following a standard that I defined.
I would like to add an option to change the color of the plots according to a color order that I defined. If the figure has only one plot it's easy, I can modify the color property of each Line found with findobj, but I'm stuck with the figures having subplots.
I was thinking to use the ColorOrder option taken from the axes properties but the figure does not 'refresh' with the new colors (I tried also to use refresh( ... ) ).
Another idea I had was to modify the color Line by Line but I'm not able to distinguish to which subplot they belong (in theory every subplot should restart the color order).
Here is an idea of the outcome where on the left there is subplot(121) not yet modified while in subplot(122) there is the plot with the new color order. At the end subplot(121) and subplot(122) should have the same colors.


Can you not just get the axes handles as being children of the figure and then get the line handles as children of each axes? Or you can use findobj on the axes if you want, but I would just use the Children in this case (normally I would keep hold of the handles of the things I plot, but obviously this isn't an option working with loaded .fig)
Your comment is correct, I didn't know I could have the lines data starting from the axes handles. I though the axes handles had only properties strictly related to the axes while to get the lines I had to use findobj.
The axes properties do strictly relate to the axes, but since one of those is 'Children', this contains the objects that are parented by the axes (whether they be lines, images, scatter plots, etc) so they when you retrieve them they can be interrogated themselves.
doc inspect
is an excellent tool for playing around with graphics objects and seeing more visually what you can access and how changing them affects the figure.
inspect( hAxes )
will show you the axes properties in a dialog where they are easily editable. The end effect is no different to changing them programatically, but it is often faster when you just want to look around at what can be changed and what its effect is.

Sign in to comment.

1 Answer

Answer by José-Luis
on 31 Aug 2017
Edited by José-Luis
on 31 Aug 2017
 Accepted Answer

Creating figure and forgetting all handles:
for ii = 1:2
close all
Loading figure:
fH = openfig('dummy.fig');
colorArray = eye(3);
numColors = size(colorArray,1);
%Getting axes objects
aH = findall(fH,'Type','axes').';
for ii = aH
lH = findall(ii,'Type','line').';
cnt = 0;
for jj = lH
idx = mod(cnt,numColors) + 1;
jj.Color = colorArray(idx,:);
jj.LineWidth = 2;
cnt = cnt + 1;
Your mileage may vary. Off the top of my head, it will not work if there are lines that are not visible. You'd need to adapt for that.
If you are not loading a figure but generating it, keeping track of your handles is the sane solution.

  1 Comment

Thanks for your answer, this is the outcome I was searching for. I didn't know you could extract line type of data from axes properties.
The function I'm creating is intended to modify report plots. Let's say the editor is asking me a different color scheme or font I prefer to load the figures instead of modifying the post processing which generates them. In another case I agree with you the sane solution is saving the handles!
Thanks again for your help!

Sign in to comment.