82 views (last 30 days)
Show older comments
Aditya Zade on 2 Aug 2024 at 23:51
Commented: Walter Roberson on 3 Aug 2024 at 20:15
This question was flagged by dpb
Show flagsHide flags
- Flagged as Not appropriate for MATLAB Answers by dpb on 3 Aug 2024 at 17:19.
Don't use responders as tags, @Aditya Zade
- Fig 1.fig
- Fig 2.fig
I would like to copy the properties from Fig 1 to Fig 2. How can that be done?
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (1)
Walter Roberson on 3 Aug 2024 at 17:41
Edited: Walter Roberson on 3 Aug 2024 at 17:43
Open in MATLAB Online
- Fig 1.fig
- Fig 2.fig
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 Show 8 older commentsHide 8 older comments
Show 8 older commentsHide 8 older comments
Aditya Zade on 3 Aug 2024 at 18:21
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2142591-how-do-i-copy-figure-properties-from-one-to-another#comment_3228551
Edited: Aditya Zade on 3 Aug 2024 at 18:22
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.
Umar on 3 Aug 2024 at 18:46
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2142591-how-do-i-copy-figure-properties-from-one-to-another#comment_3228591
Edited: Walter Roberson on 3 Aug 2024 at 18:56
Open in MATLAB Online
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
https://www.mathworks.com/help/matlab/ref/copyobj.html
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 at 18:50
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2142591-how-do-i-copy-figure-properties-from-one-to-another#comment_3228601
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.
Walter Roberson on 3 Aug 2024 at 18:59
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2142591-how-do-i-copy-figure-properties-from-one-to-another#comment_3228616
Open in MATLAB Online
% 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');
Aditya Zade on 3 Aug 2024 at 19:28
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2142591-how-do-i-copy-figure-properties-from-one-to-another#comment_3228646
Edited: Aditya Zade on 3 Aug 2024 at 19:29
Open in MATLAB Online
- New Fig 2.fig
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');
Umar on 3 Aug 2024 at 19:35
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2142591-how-do-i-copy-figure-properties-from-one-to-another#comment_3228661
@Walter,
I do really appreciate your help by pointing this out.
Umar on 3 Aug 2024 at 19:39
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2142591-how-do-i-copy-figure-properties-from-one-to-another#comment_3228671
Edited: Walter Roberson on 3 Aug 2024 at 19:49
Open in MATLAB Online
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 at 19:58
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2142591-how-do-i-copy-figure-properties-from-one-to-another#comment_3228701
Open in MATLAB Online
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 at 20:11
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2142591-how-do-i-copy-figure-properties-from-one-to-another#comment_3228716
Open in MATLAB Online
- New Fig 2.fig
- indp.png
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');
Walter Roberson on 3 Aug 2024 at 20:15
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/2142591-how-do-i-copy-figure-properties-from-one-to-another#comment_3228721
The legend of the plot is not a figure property: it is an axes property.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsPrinting and Saving
Find more on Printing and Saving in Help Center and File Exchange
Tags
- star strider
- walter roberson
- torsten
- dyuman joshi
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office