MATLAB Answers

How do I save a figure with linked properties?

14 views (last 30 days)
Fabian Gock
Fabian Gock on 6 Aug 2018
Commented: Adam Danz on 7 Aug 2018
I have a figure with two 3 dimensional axes that are linked via
linkprop([ax2 ax1], {'View', 'XLim','YLim','ZLim'})
I used two sets of axes, because i need two different colormaps for a imagesc and hist3 plot, that are overlaying.
When I save the figure using savefig, and open it again, linkprop is resetted. How can I save the figure with the linked properties?
Thanks in advance! -Fabian


Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 6 Aug 2018
Edited: Adam Danz on 6 Aug 2018
The documentation on linkprop recommends storing the link object that linkprop() produces in the object's UserData property. After re-opening the figure you could then access the linkprop object to restore its properties. Here is a simple example where I create two figures and link the properties of the first to the second. Then I save the 2nd figure and close both figures. Then I open the 2nd figure and apply the link object to it again. However, in this simple example when I open the 2nd figure the properties are maintained which makes me wonder how you're saving your figure; using savefig()?
% Create 2 figs; we'll use the first as the template
f1 = figure('name', 'fig1', 'color', 'b', 'NumberTitle', 'off');
f2 = figure('name', 'fig2');
% Copy properties to fig 2; store link object in fig 2
linkObj = linkprop([f1, f2], {'Color', 'NumberTitle'});
f2.UserData = linkObj;
% save figure 2 to current directory; close both figure and clear vars
savefig(f2, 'fig2')
clear all
% open figure 2
f2 = open('fig2.fig');
% Restore properties stored in UserData
addtarget(f2.UserData, f2)


Fabian Gock
Fabian Gock on 7 Aug 2018
First of all, thanks for your answer. This is actually not exactly addressing my problem.
See this example:
%create data
[X,Y,Z] = peaks(30);
Zprime = del2(Z);
contourmin = min(Z(:));
contourmax = max(Z(:));
pcolormin = min(Zprime(:));
pcolormax = max(Zprime(:));
%create figure and store handle
hF = figure;
%create axes for pcolor and store handle
hAxesP = axes;
%set colormap for pcolor axes
%plot pcolor for gradient
pcolorPlot = pcolor(X,Y,Zprime);
%create color bar and set range for color
cbP = colorbar(hAxesP,'Location','east');
caxis(hAxesP,[pcolormin pcolormax]);
%create axes for the countourm axes
hAxesCM = axes;
%set visibility for axes to 'off' so it appears transparent
%set colormap for contourm axes
%plot contourm
contourmPlot = contour3(X,Y,Z,20);
%create color bar and set range for color
cbCM = colorbar(hAxesCM,'Location','west');
caxis(hAxesCM,[contourmin contourmax]);
%link the two overlaying axes so they match at all times to remain accurate
This makes both plots rotate simultaneously. Bus when I save the figure, using savefig(), and then open it again, the linked properties are resetted and both axes rotate independently when I drag them with my mouse.
linkaxes seems to maintain the link after reopening it (that's what i read) but does not work with 3D axes.
Fabian Gock
Fabian Gock on 7 Aug 2018
You actually helped me out a lot.
linkObj = linkprop([f1, f2], {'Color', 'NumberTitle'});
f2.UserData = linkObj;
solved my problem. Thanks!
Adam Danz
Adam Danz on 7 Aug 2018
Good! Its interesting that storing the linkObj output isn't necessary for a simple 2D plot but is needed for the 3D axis limits. Hmmm.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by