How can i merge three mat.fig in a single figure as subplots which has multiple axis

Hallo Everyone,
I am trying to merge three mat.fig in a single figure as subplots which has multiple axis. But when i run it using the following code all the three figures are overlapping on each other. My code is as follows:
% Load saved figures
Fig1=hgload('Trail1.fig');
Fig2=hgload('Trail2.fig');
Fig3=hgload('Trail3.fig');
% Prepare the new figure and layout
f = figure;
t = tiledlayout(f,2,2);
% Paste figures on the tiles
child1 = copyobj(Fig1.Children, t);
child2 = copyobj(Fig2.Children, t);
child3 = copyobj(Fig3.Children, t);
ax1 = findobj(child1, 'type', 'axes');
ax2 = findobj(child2, 'type', 'axes');
ax2.Layout.Tile = 2; % Move the axes to the right tile.
Intermediate dot '.' indexing produced a comma-separated list with 2 values, but it must produce a single value when followed by subsequent indexing operations.
ax3 = findobj(child3, 'type', 'axes');
ax3.Layout.Tile = 3; % Move the axes to the right tile.
% manually set the Colormap property of "Axes" in each panel
ax1.Colormap = Fig1.Colormap;
ax2.Colormap = Fig2.Colormap;
ax3.Colormap = Fig3.Colormap;
I am attaching the code and the .fig files. Thanks in advance.
With regards

Respuestas (1)

The error get thrown because each .fig contains more than one axes object, so ax2 is not a scalar. When you write:
ax2.Layout.Tile = 2;
MATLAB sees ax2.Layout as a comma-separated list from multiple axes, which causes the error.
Adjust as required:
figFiles = {'Trail1.fig','Trail2.fig','Trail3.fig'};
% New combined figure
f = figure('Color','w');
% Optional: avoid depending on graphics hardware
set(f,'Renderer','painters')
% Manual 2-by-2 layout positions:
% [left bottom width height]
pos = [
0.08 0.55 0.38 0.38 % top left
0.56 0.55 0.38 0.38 % top right
0.08 0.08 0.38 0.38 % bottom left
0.56 0.08 0.38 0.38 % bottom right, legend
];
% Storage for legend entries
legLabels = {};
legStyleObjects = gobjects(0);
for k = 1:numel(figFiles)
oldFig = openfig(figFiles{k}, 'invisible');
% Panel for this figure
pnl = uipanel('Parent', f, ...
'Units', 'normalized', ...
'Position', pos(k,:), ...
'BorderType', 'none', ...
'BackgroundColor', 'w');
% Get real axes only
oldAx = findall(oldFig, 'Type', 'axes');
% Remove old-style legend/colorbar axes, if present
oldAx = oldAx(~strcmp(get(oldAx,'Tag'), 'legend'));
oldAx = oldAx(~strcmp(get(oldAx,'Tag'), 'Colorbar'));
% Copy axes only
copyobj(oldAx, pnl);
% Fix copied axes
newAx = findall(pnl, 'Type', 'axes');
for n = 1:numel(newAx)
try
newAx(n).Color = 'none';
newAx(n).Units = 'normalized';
catch
end
try
colormap(newAx(n), oldFig.Colormap);
catch
end
end
% Extract legend information from the original figure
oldLeg = findall(oldFig, 'Type', 'legend');
for m = 1:numel(oldLeg)
labels = oldLeg(m).String;
if ischar(labels)
labels = cellstr(labels);
end
% Try to get the objects used by the legend
try
hLegObj = oldLeg(m).PlotChildren;
catch
hLegObj = gobjects(0);
end
% Store labels and representative style objects
for q = 1:numel(labels)
thisLabel = labels{q};
% Avoid duplicate legend labels
if ~any(strcmp(legLabels, thisLabel))
legLabels{end+1} = thisLabel;
if q <= numel(hLegObj)
legStyleObjects(end+1) = hLegObj(q);
else
legStyleObjects(end+1) = gobjects(1);
end
end
end
end
close(oldFig)
end
% Create a dedicated invisible axes in position (2,2)
legAx = axes('Parent', f, ...
'Units', 'normalized', ...
'Position', pos(4,:), ...
'Visible', 'off');
hold(legAx, 'on')
% Create dummy objects for the legend
dummy = gobjects(numel(legLabels),1);
for q = 1:numel(legLabels)
src = legStyleObjects(q);
% Default dummy line
dummy(q) = plot(legAx, nan, nan, ...
'DisplayName', legLabels{q}, ...
'LineWidth', 1.5);
% Try to copy style from the original legend object
if isgraphics(src)
try
dummy(q).Color = src.Color;
end
try
dummy(q).LineStyle = src.LineStyle;
end
try
dummy(q).Marker = src.Marker;
end
try
dummy(q).MarkerSize = src.MarkerSize;
catch
try
dummy(q).LineWidth = src.LineWidth;
end
end
end
% Place the legend in the bottom-right panel/position
lgd = legend(legAx, dummy, legLabels, ...
'Location', 'northwest');
lgd.Box = 'off';
lgd.Units = 'normalized';
Warning: Graphics acceleration hardware is unavailable. Graphics quality and performance might be diminished. See MATLAB System Requirements.

2 comentarios

Hello @Stephen23,
Thank you for the quick response. But as you see from the subplot, only one plot is shown in each subplot. You can see the mat.fig has two axes in each and also the legend is missing. How can i update that.
Thank you for the support. It works pretty well as required. Thank you once again.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by