I have a code which goes like this:
clear; close all; clc;
SizeOfAxesAndPlotLine = 1.5;
SizeOfAxesAndLegendFont = 12;
%% 1st figure
x1 = 1:0.01:2;
y1 = 10:0.1:20;
figure('units','normalized','outerposition',[0 0 1 1]) % For full screen
subplot(3,1,1)
plot(x1,y1,'LineWidth',SizeOfAxesAndPlotLine)
set(gca,'linewidth',SizeOfAxesAndPlotLine)% Changes width of the axes
xlabel('x values');
ylabel('y values');
legend({'Y1 Values'},'FontSize',SizeOfAxesAndLegendFont)
title('Plot # 1')
ax = gca;
ax.FontSize = SizeOfAxesAndLegendFont;
ax.YScale = 'log';
%% 2nd figure
x2 = 2:0.01:3;
y2 = 20:0.1:30;
subplot(3,1,2)
plot(x2,y2,'LineWidth',SizeOfAxesAndPlotLine)
set(gca,'linewidth',SizeOfAxesAndPlotLine)% Changes width of the axes
xlabel('x values');
ylabel('y values');
legend({'Y2 Values'},'FontSize',SizeOfAxesAndLegendFont)
title('Plot # 2')
ax = gca;
ax.FontSize = SizeOfAxesAndLegendFont;
ax.YScale = 'log';
%% 3rd figure
x3 = 3:0.01:4;
y3 = 30:0.1:40;
subplot(3,1,3)
p3 = plot(x3,y3,'LineWidth',SizeOfAxesAndPlotLine);
set(gca,'linewidth',SizeOfAxesAndPlotLine)% Changes width of the axes
xlabel('x values');
ylabel('y values');
legend({'Y3 Values'},'FontSize',SizeOfAxesAndLegendFont)
title('Plot # 3')
ax = gca;
ax.FontSize = SizeOfAxesAndLegendFont;
ax.YScale = 'log';
% print('-dpng','Figure1');
%% 4th figure
x4 = 10:0.01:20;
y4 = 100:0.1:200;
figure('units','normalized','outerposition',[0 0 1 1]) % For full screen
subplot(3,1,1)
p4 = plot(x4,y4,'LineWidth',SizeOfAxesAndPlotLine);
set(gca,'linewidth',SizeOfAxesAndPlotLine)% Changes width of the axes
xlabel('x values');
ylabel('y values');
legend({'Y4 Values'},'FontSize',SizeOfAxesAndLegendFont)
title('Plot # 4')
ax = gca;
ax.FontSize = SizeOfAxesAndLegendFont;
ax.YScale = 'log';
%% 5th figure
x5 = 20:0.01:30;
y5 = 200:0.1:300;
subplot(3,1,2)
plot(x5,y5,'LineWidth',SizeOfAxesAndPlotLine)
set(gca,'linewidth',SizeOfAxesAndPlotLine)% Changes width of the axes
xlabel('x values');
ylabel('y values');
legend({'Y5 Values'},'FontSize',SizeOfAxesAndLegendFont)
title('Plot # 5')
ax = gca;
ax.FontSize = SizeOfAxesAndLegendFont;
ax.YScale = 'log';
%% 6th figure
x6 = 20:0.01:30;
y6 = 200:0.1:300;
subplot(3,1,3)
plot(x6,y6,'LineWidth',SizeOfAxesAndPlotLine)
set(gca,'linewidth',SizeOfAxesAndPlotLine)% Changes width of the axes
xlabel('x values');
ylabel('y values');
legend({'Y6 Values'},'FontSize',SizeOfAxesAndLegendFont)
title('Plot # 6')
ax = gca;
ax.FontSize = SizeOfAxesAndLegendFont;
ax.YScale = 'log';
% print('-dpng','Figure2');
This produce 2 figures which are as shown:
figure('units','normalized','outerposition',[0 0 1 1]) % For full screen
subplot(2,1,1)
p3
subplot(2,1,2)
p4
ylabel('y values');
six times. Is it possible that one can assign a variable (say ylab
) to it and then use it wherever I want?I am using Matlab 2016a.
Let's start from the second part - yes, easily. Just assign the string to a variable:
yLable = 'y values';
And the write:
ylabel(yLable);
As for the first question, you could go like this:
figure('units','normalized','outerposition',[0 0 1 1]) % For full screen
ax = subplot(2,1,1);
copyobj(p3,ax)
ax = subplot(2,1,2);
copyobj(p4,ax)
Here, you get the handle of the subplot (in ax
) and copy into it the lines with their chosen handle (p3,p4
), using the copyobj
function.
However, this will not copy the whole axes (with labels and so on...), but only the line. If you want to copy the axes you need to go one step higher in the hierarchy and give your figure a handle (f3
below). Also you will need to get the axes handle in the following lines in your code:
...
ax3 = subplot(3,1,3) %<-note the assignment
p3=plot(x3,y3,'LineWidth',SizeOfAxesAndPlotLine)
...
ax4 = subplot(3,1,1) %<-note the assignment
p4=plot(x4,y4,'LineWidth',SizeOfAxesAndPlotLine)
...
Then copy the axes into the figure:
f3 = figure('units','normalized','outerposition',[0 0 1 1]) % For full screen
copyobj(ax3,f3);
copyobj(ax4,f3);
This will retain every property of the axes, including its position in the figure, which probably not needed here (since you call subplot
with different arguments). To deal with that we can create empty axes with subplot
, grab its position, assign the position to the copied axes and delete it. We do that for each axes copied:
f3 = figure('units','normalized','outerposition',[0 0 1 1]) % For full screen
axPos = subplot(2,1,1); % dummy axes 1
ax = copyobj(ax3,f3);
ax.Position = axPos.Position; % set the position to dummy axes 1
delete(axPos) % delete dummy axes 1
axPos = subplot(2,1,2); %<-dummy axes 2
ax = copyobj(ax4,f3);
ax.Position = axPos.Position; % set the position to dummy axes 2
delete(axPos) % delete dummy axes 2
And we get: