Question
I have two codes. Each code has four graphs. I want to plot two graphs in one figure. For example: Dead nodes vs Round graph of two should be in one figure. In the same way other graphs also. I tried hold on function but still not getting. How to merge the two codes in order to get the graphs ? Code 1: clcclear all;close all;xm=100;ym=100;x=0;y=0;sink.x=0.5*xm; %location of sink on x-axissink.y=0.9*ym; %location of sink on y-axisn=100 %nodesP=0.1 ; %probability of cluster headsEo=0.5; %initial energyETX=50*0.000000001; %tx energyERX=50*0.000000001; %rx energyEfs=10*0.000000000001; %free space lossEmp=0.0013*0.000000000001; %multi path loss%Data Aggregation EnergyEDA=5*0.000000001; %compression energya=1; %fraction of energy enhancment of advance nodesrmax=3000 %maximum number of roundsdo=sqrt(Efs/Emp); %distance do is measuredEt=0; %variable just use below A=0;for i=1:1:n S(i).xd=rand(1,1)*xm; %generates a random no. use to randomly distibutes nodes on x axis XR(i)=S(i).xd; S(i).yd=rand(1,1)*ym; %generates a random no. use to randomly distibutes nodes on y axis YR(i)=S(i).yd; S(i).G=0; %node is elegible to become cluster head talha=rand*a; S(i).E=Eo*(1+talha); E(i)= S(i).E; A=A+talha; Et=Et+E(i); %estimating total energy of the network %initially there are no cluster heads only nodes S(i).type='N'; figure(10) plot(S(i).xd,S(i).yd,'bo'); text(S(i).xd+1,S(i).yd-0.5,num2str(i)); title 'Node Deployment'; xlabel 'X-Coordinate(m)'; ylabel 'Y-Coordinate(m)'; hold on;endd1=0.765*xm/2; %distance between cluster head and base stationK=sqrt(0.5*n*do/pi)*xm/d1^2; %optimal no. of cluster headsd2=xm/sqrt(2*pi*K); %distance between cluster members and cluster headEr=4000*(2*n*ETX+n*EDA+K*Emp*d1^4+n*Efs*d2^2); %energy desipated in a roundS(n+1).xd=sink.x; %sink is a n+1 node, x-axis postion of a nodeS(n+1).yd=sink.y; %sink is a n+1 node, y-axis postion of a nodecountCHs=0; %variable, counts the cluster headcluster=1; %cluster is initialized as 1flag_first_dead=0; %flag tells the first node deadflag_tenth_dead=0; %flag tells the 10th node deadflag_all_dead=0; %flag tells all nodes deaddead=0; %dead nodes count initialized to 0first_dead=0;tenth_dead=0;all_dead=0;allive=n;%counter for bit transmitted to Bases Station and to Cluster Headspackets_TO_BS=0;packets_TO_CH=0;for r=0:1:rmax r if(mod(r, round(1/P) )==0) for i=1:1:n S(i).G=0; S(i).cl=0; end endEa=Et*(1-r/rmax)/n;dead=0;for i=1:1:n if (S(i).E0 S(i).type='N'; endend
Expert Answer
Prashant Kumar
PhD Expert
Answered Aug 28, 2026
I want to plot two graphs in one figure
Options are
- subplot
- tiledlayout with nexttile - preferred, starting in R2019b
subplot(m,n,i) creates an axes in the i^th position of an m-by-n grid.
tiledlayout(m,n) creates an m-by-n grid upon which axes can be added using nexttile.
See documentation links for details.
Benefits to using tiledlayout
The following features are great improvements available in tiledlayout.
- Flexible grid sizes (TileArrangment property)
- Control spacing (TileSpacing and Padding properties, Community Highlight)
- Row-wise or column-wise order of axes (TileIndexing property, Community Highlight)
- Global labels such as a title, subtitle, xlabel and ylabel (labels properties)
- Global legends, starting in R2020b (legend layout property, documentation example, more demos)
- Global colorbars, starting in R2020b (colorbar layout property, documentation example, demo)
- Specify the span of an axes within the grid using nexttile(span)
Demo
tcl = tiledlayout(2,3,'TileSpacing','compact');
nexttile plot(magic(5)) axis tight nexttile scatter(rand(1,10), rand(1,10), 90, lines(10)) box on nexttile([2,1]) imagesc(peaks(200)) nexttile([1,2]) histogram2(2*randn(1,1000), randn(1,1000), 'FaceColor', 'Flat') cb = colorbar(); cb.Layout.Tile = 'south'; title(tcl, 'Global title') xlabel(tcl, 'Global xlabel') ylabel(tcl, 'Global ylabel')

100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related sub plot Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →