Clear Filters
Clear Filters

two matlab figures as inset

23 views (last 30 days)
omnia
omnia on 15 May 2017
Edited: Yukthi S on 3 Sep 2024 at 10:33
Hello all I have 4 saved matlab figures (.fig) and i want three to be the inset of one. How can I do this.
  1 Comment
Cam Salzberger
Cam Salzberger on 15 May 2017
It is not clear what you mean by "inset". Do you mean that you have four figures, each containing one axes, and you would like to have one figure containing four axes (like subplot)? Or that you have four figures with a bunch of stuff on them, and you would like to change it to one figure with four uitabs?

Sign in to comment.

Answers (1)

Yukthi S
Yukthi S on 3 Sep 2024 at 10:32
Edited: Yukthi S on 3 Sep 2024 at 10:33
Hi @omnia,
I assume that you want to create a main figure in MATLAB with three inset plots in it from the saved .FIG files.
To do so, you can follow these steps:
Step-1: Open the saved figures using openfig in the MATLAB code.
Step-2: Create a new figure to serve as main plot.
Step-3: Use the axes function to create inset axes within the main figure.
Step-4: Copy the contents of the loaded figures into the main figure and the inset axes using copyobj.
You can refer to the following MathWorks documentation links to get more insights on copyobj and openfig:
Hers is a sample code to help with the creation of 3 insets inside a main plot:
% Load the saved .fig files
fig1 = openfig('figure1.fig', 'invisible');
fig2 = openfig('figure2.fig', 'invisible');
fig3 = openfig('figure3.fig', 'invisible');
fig4 = openfig('figure4.fig', 'invisible');
% Create a new figure for the combined(main plot) plot
mainFig = figure;
% Create main axes in the new figure
mainAxes = axes('Parent', mainFig, 'Position', [0.1, 0.1, 0.8, 0.8]);
% Copy the contents of fig1 to the main axes
axes1Children = get(get(fig1, 'CurrentAxes'), 'Children');
copyobj(axes1Children, mainAxes);
% Close the original figure
close(fig1);
% Create inset axes for fig2, fig3, and fig4
inset1 = axes('Parent', mainFig, 'Position', [0.15, 0.7, 0.2, 0.2]);
inset2 = axes('Parent', mainFig, 'Position', [0.4, 0.7, 0.2, 0.2]);
inset3 = axes('Parent', mainFig, 'Position', [0.65, 0.7, 0.2, 0.2]);
% Copy the contents of fig2, fig3, and fig4 to the inset axes
axes2Children = get(get(fig2, 'CurrentAxes'), 'Children');
copyobj(axes2Children, inset1);
axes3Children = get(get(fig3, 'CurrentAxes'), 'Children');
copyobj(axes3Children, inset2);
axes4Children = get(get(fig4, 'CurrentAxes'), 'Children');
copyobj(axes4Children, inset3);
% Close the original figures
close(fig2);
close(fig3);
close(fig4);
% Optional: Adjust properties of the main and inset axes
set(mainAxes, 'Box', 'on');
set(inset1, 'Box', 'on');
set(inset2, 'Box', 'on');
set(inset3, 'Box', 'on');
% Optional:Set titles, labels, or other properties as needed
%title(mainAxes, 'Main Plot');
title(inset1, 'Inset 1');
title(inset2, 'Inset 2');
title(inset3, 'Inset 3');
annotation('textbox', [0.1, 0.05, 0.8, 0.05], 'String', 'Main Plot', ...
'HorizontalAlignment', 'center', 'EdgeColor', 'none', 'FontSize', 12);
Make sure the saved figures are on the same path as the MATLAB code before running the code.

Tags

Community Treasure Hunt

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

Start Hunting!