open saved figures and plot daily data in one graph
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Eswaramoorthy
on 25 Apr 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!
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!