How to plot the subplots but with less lines of code?

3 views (last 30 days)
Hi,
I am plotting the 4 graphs in subplotting using the code below. but I want to use less amount of lines. Code is below!
Code:
files = ['F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_Head\Data_Raw\LaserSheet.xlsx'];
a = readtable(files);
x = a{:,16};
y = a{:,15};
files = ['F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_C\Data_Head\Data_Raw\LaserSheet.xlsx']
a = readtable(files);
x1 = a{:,16};
y1 = a{:,15};
files = ['F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_B\Data_Head\Data_Raw\LaserSheet.xlsx'];
a = readtable(files);
x2 = a{:,16};
y2 = a{:,15};
files = ['F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_A\Data_Head\Data_Raw\LaserSheet.xlsx'];
a = readtable(files);
x3 = a{:,16};
y3 = a{:,15};
figure()
subplot(2,2,1)
plot(x,y,'*')
%Spacing and griding
xlim([0 6])
ylim([0 600])
hold on
grid on
% Design Head
y = 500;
line ([0,500],[y,y],'color','k','LineWidth',1);
txt = {'Design Head = 500 mm'};
text(1,560,txt)
x = 5;
line ([x,x],[0,500],'color','k','LineWidth',1);
%Chart Representation
title('DataLaserSheet-D');
xlabel('flowrate (l/s)');
ylabel('Head (mm))');
hold on
subplot(2,2,2)
plot(x1,y1,'*')
%Spacing and griding
xlim([0 6])
ylim([0 600])
hold on
grid on
% Design Head
y = 500; %17 mm
line ([0,500],[y,y],'color','k','LineWidth',1);
txt = {'Design Head = 500 mm'};
text(1,560,txt)
x = 5;
line ([x,x],[0,500],'color','k','LineWidth',1);
%Chart Representation
title('DataLaserSheet-C');
xlabel('flowrate (l/s)');
ylabel('Head (mm))');
hold on
subplot(2,2,3)
plot(x2,y2,'*')
hold on
%Spacing and griding
xlim([0 6])
ylim([0 600])
hold on
grid on
% Design Head
y = 500; %17 mm
line ([0,500],[y,y],'color','k','LineWidth',1);
txt = {'Design Head = 500 mm'};
text(1,560,txt)
x = 5;
line ([x,x],[0,500],'color','k','LineWidth',1);
%Chart Representation
title('DataLaserSheet-B');
xlabel('flowrate (l/s)');
ylabel('Head (mm))');
subplot(2,2,4)
plot(x3,y3,'*')
hold on
%Spacing and griding
xlim([0 6])
ylim([0 600])
hold on
grid on
%Design,Kickback and Flushflow ppoints
% Design Head
y = 500; %17 mm
line ([0,500],[y,y],'color','k','LineWidth',1);
txt = {'Design Head = 500 mm'};
text(1,560,txt)
x = 5;
line ([x,x],[0,500],'color','k','LineWidth',1);
%Chart Representation
title('DataLaserSheet-A');
xlabel('flowrate (l/s)');
ylabel('Head (mm))');
%over all chart title
sgtitle('Outlet-110 mm')

Accepted Answer

David Goodmanson
David Goodmanson on 7 Jan 2022
HI muhammed
all you need to do is put the subplots into a for loop and change the things that vary with subplot. That is the data of course, and also the sheet letter, A through D. Putting the x and y data into matrices allows them to be addressed by means of the for loop variable j:
% after obtaining the data, concatenate it into matrices
xm = [x x1 x2 x3];
ym = [y y1 y2 y3];
sheetlabel = 'DCBA'
figure(1)
for j = 1:4
subplot(2,2,j)
plot(xm(:,j),ym(:,j),'*')
%Spacing and griding
xlim([0 6])
ylim([0 600])
hold on
grid on
% Design Head
yy = 500;
line ([0,500],[yy,yy],'color','k','LineWidth',1);
txt = {'Design Head = 500 mm'};
text(1,560,txt)
xx = 5;
line ([xx,xx],[0,500],'color','k','LineWidth',1);
%Chart Representation
title(['DataLaserSheet-',sheetlabel(j)]);
xlabel('flowrate (l/s)');
ylabel('Head (mm))');
hold off
end

More Answers (1)

Image Analyst
Image Analyst on 14 Jan 2022
Try using the nexttile() function
nexttile;
instead of the subplot() function
subplot(2, 3, j)

Categories

Find more on Data Distribution 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!