open saved figures and plot daily data in one graph

1 visualización (últimos 30 días)
Cakil
Cakil el 17 de Abr. de 2023
Respondida: Eswaramoorthy el 25 de Abr. de 2023
Dear Matlab Community,
I am trying to create one graph from previously saved 2 figures using the same x axis.
x axis shows '1 Jan' from two different years. My aim to see the data in one plot, however I do not know how to combine them. I am trying to use the below script:
fig1 = openfig('Figure1.fig');
fig2 = openfig('Figure2.fig');
xlimits = get(fig1.CurrentAxes, 'XLim');
xticks = get(fig1.CurrentAxes, 'XTick');
% Create a new figure with a single axes object
figure;
ax = axes;
% Apply the x-axis limits and tick values from Figure 1 to the new axes object
set(ax, 'XLim', xlimits);
set(ax, 'XTick', xticks);
% Copy the plots from each figure to the new axes object
copyobj(allchild(get(fig1,'CurrentAxes')), ax);
hold on;
copyobj(allchild(get(fig2,'CurrentAxes')), ax);
hold off
% Set the axis labels and legend as needed
xlabel('Time');
ylabel('Data 1');
title('2011 - 2012 Comparison');
legend('2011', '2012');
Here I have found a similar question but I am totally confused. I really appreciate your help.
Best, Cakil

Respuestas (1)

Eswaramoorthy
Eswaramoorthy el 25 de Abr. de 2023
Hi,
Here is the code for your graph:
fig1 = openfig('Figure1.fig');
h = findobj(gca,'Type','line')
x1 = h.XData;
y1 = h.YData;
fig2 = openfig('Figure2.fig');
h = findobj(gca,'Type','line')
x2 = h.XData ;
y2 = h.YData;
xData=[x1',x2'];
yData=[y1',y2'];
yr=[];
% Storing dates in MDdt1 after setting the year of all points to
% '0001', so that all points can be plotted in the same graph.
% Storing years in the array 'yr', which is later used for legend.
for k = 1:size(xData,2)
[y,m,d] = ymd(xData(:,k));
MDdt1(:,k) = datetime(1,m,d);
yr = [yr,string(y(1))];
end
figure
hold on
for k = 1:size(MDdt1,2)
plot(MDdt1(:,k), yData(:,k), 'DisplayName',yr(k));
end
grid
xtickformat('dd/MM')
legend('Location','best')
xlabel('dd/MM')
ylabel('Daily Count')
Hope this helps!

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by