How do I copy figure properties from one to another?

Answers (1)

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

Oh, I think my question was ambiguous. 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.
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.
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
ans = 0x0 empty char array
% 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
Error using matlab.ui.Figure/set
Error setting property 'CurrentCharacter' of class 'Figure':
Value must be a scalar char.
% Display Fig 2
set(fig2, 'Visible', 'on');
Thank you very much for responding.
However, when i am using your code, Walter, I get a new figure called "New Fig 2" with two figures inside. But, the data of original Fig 2 also gets replaced by the data of original Fig 1. I have attached the "New Fig 2" for your reference.
I am using this code:
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');
@Walter,
I do really appreciate your help by pointing this out.
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.
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');
I might be doing someting wrong, but this code is still not copying properties into the newly created figure. The newly created figure is attached. This figure should look like the png that I have attached. I have manually entered all the properties to get this png.
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');
The legend of the plot is not a figure property: it is an axes property.

Sign in to comment.

Categories

Tags

Asked:

on 2 Aug 2024

Commented:

on 3 Aug 2024

Community Treasure Hunt

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

Start Hunting!