How do I copy figure properties from one to another?
Show older comments
I would like to copy the properties from Fig 1 to Fig 2. How can that be done?
Answers (1)
Walter Roberson
on 3 Aug 2024
Edited: Walter Roberson
on 3 Aug 2024
The below code sets all sensible properties.
fig1 = openfig('Fig 1.fig');
fig2 = openfig('Fig 2.fig');
propnames = fieldnames(set(fig1));
if strcmp(fig2.WindowStyle, 'docked')
propnames = propnames(~ismember(propnames, ...
{'InnerPosition', ...
'OuterPosition', ...
'Position'}));
end
propnames = propnames(~ismember(propnames, ...
{ 'Children', ...
'CurrentAxes', ...
'CurrentCharacter', ...
'CurrentObject', ...
'CurrentPoint', ...
'FileName', ...
'IntegerHandle', ...
'Parent', }));
set(fig2, propnames, get(fig1, propnames));
savefig(fig2, 'New Fig 2.fig');
10 Comments
Aditya Zade
on 3 Aug 2024
Edited: Aditya Zade
on 3 Aug 2024
Umar
on 3 Aug 2024
Edited: Walter Roberson
on 3 Aug 2024
To address your query, “I would like to implement all the figure properties from Fig 1 to Fig 2. Instead of changing the figure properties in Fig 2 manually, it would be nice if I can just copy figure properties of Fig 1 and paste those into Fig 2.”
% Get the figure properties of Fig 1
fig1 = figure('Visible', 'off'); % Create a temporary figure
copyobj(findobj('type','axes'), fig1); % Copy axes objects
fig1Props = get(fig1);
% Apply the figure properties to Fig 2
fig2 = figure('Visible', 'off'); % Create Fig 2
set(fig2, fig1Props); % Set Fig 2 properties same as Fig 1
% Display Fig 2
set(fig2, 'Visible', 'on');
So, as you can see the code snippet creates a temporary figure (Fig 1) to extract its properties and then applies those properties to a new figure (Fig 2). Finally, it displays Fig 2 with the copied properties. For more guidance on “copyobj” function, please refer to
Hope this answers your question. Although, @Walter Robertson did answer your first question and he is very humble, doesn’t brag about his accomplishments, has years of experience and helping out us and even resolved many challenging problems. So, If I were you, I will take his advice into consideration as well.
Walter Roberson
on 3 Aug 2024
You cannot copy all of the figure properties from Fig 1 to Fig 2.
- there are a number of figure properties that simply cannot be set (read-only properties)
- it does not make any sense to copy properties such as CurrentAxes, as they would refer to objects that exist in the first figure rather than objects that exist in the second figure
- copying the Children property would be equivalent to copying the contents of the figure
- the second figure has window style 'docked'. It is not permitted to set the various Position properties of something that is docked
it would be nice if I can just copy figure properties of Fig 1 and paste those into Fig 2.
It might be nice, but it isn't going to happen.
The code I provided copies all reasonable properties from figure 1 to figure 2.
% Get the figure properties of Fig 1
fig1 = figure('Visible', 'off'); % Create a temporary figure
copyobj(findobj('type','axes'), fig1); % Copy axes objects
fig1Props = get(fig1);
fig1Props.CurrentCharacter
% Apply the figure properties to Fig 2
fig2 = figure('Visible', 'off'); % Create Fig 2
set(fig2, fig1Props); % Set Fig 2 properties same as Fig 1
% Display Fig 2
set(fig2, 'Visible', 'on');
Aditya Zade
on 3 Aug 2024
Edited: Aditya Zade
on 3 Aug 2024
Umar
on 3 Aug 2024
@Walter,
I do really appreciate your help by pointing this out.
Umar
on 3 Aug 2024
Edited: Walter Roberson
on 3 Aug 2024
Hi @ Aditya Zade,
Please see updated code below, I modified the code and executed in matlab showing no errors which I should have done in the first place.
% Get the figure properties of Fig 1
fig1 = figure('Visible', 'off'); % Create a temporary figure
copyobj(findobj('type','axes'), fig1); % Copy axes objects
fig1Props = get(fig1);
% List all figure properties
fig1PropsList = get(fig1);
% Set Fig 2 properties same as Fig 1
fig2 = figure('Visible', 'off'); % Create Fig 2
set(fig2, 'Name', fig1PropsList.Name, ... % Set figure name
'NumberTitle', fig1PropsList.NumberTitle, ... % Set number title
'Color', fig1PropsList.Color, ... % Set figure color
'Position', fig1PropsList.Position, ... % Set figure position
'PaperPosition', fig1PropsList.PaperPosition, ... % Set paper position
'PaperSize', fig1PropsList.PaperSize, ... % Set paper size
'PaperType', fig1PropsList.PaperType); % Set paper type
% Display Fig 2
set(fig2, 'Visible', 'on');
By executing this code, Fig 2 will inherit the properties of Fig 1, allowing for a seamless transfer of figure properties without manual intervention. Hope, this answers your question.
Walter Roberson
on 3 Aug 2024
fig1 = openfig('Fig 1.fig');
fig2 = openfig('Fig 2.fig');
fig3 = copyobj(fig2, groot);
propnames = fieldnames(set(fig1));
if strcmp(fig2.WindowStyle, 'docked')
propnames = propnames(~ismember(propnames, ...
{'InnerPosition', ...
'OuterPosition', ...
'Position'}));
end
propnames = propnames(~ismember(propnames, ...
{ 'Children', ...
'CurrentAxes', ...
'CurrentCharacter', ...
'CurrentObject', ...
'CurrentPoint', ...
'FileName', ...
'IntegerHandle', ...
'Parent', }));
set(fig3, propnames, get(fig1, propnames));
savefig(fig3, 'New Fig 2.fig', 'compact');
Aditya Zade
on 3 Aug 2024
Walter Roberson
on 3 Aug 2024
The legend of the plot is not a figure property: it is an axes property.
Categories
Find more on Axes Appearance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!