How to change color from a .fig file having subplots

22 ビュー (過去 30 日間)
Emanuele Rondanina
Emanuele Rondanina 2017 年 8 月 31 日
編集済み: Adam 2017 年 8 月 31 日
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.
  3 件のコメント
Adam
Adam 2017 年 8 月 31 日
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.
e.g.
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.

サインインしてコメントする。

採用された回答

José-Luis
José-Luis 2017 年 8 月 31 日
編集済み: José-Luis 2017 年 8 月 31 日
Creating figure and forgetting all handles:
for ii = 1:2
subplot(1,2,ii);
plot(1:10,sort(rand(10)));
end
savefig('dummy')
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;
end
end
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 件のコメント
Emanuele Rondanina
Emanuele Rondanina 2017 年 8 月 31 日
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!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by