How can I change the title of a figure through each iteration of the loop?
16 views (last 30 days)
Show older comments
I have the following Matlab code that gives me four figures.
close all;
clear variables;
clc;
% % For Phi/Psi = +/-10
CoverageArea_mean_10 = {[84.4735,21.1779,6.4247,2.1416],[45.2112,8.8043,1.1898,0]};
CoverageArea_min_10 = {[98.5128,21.1779,6.9007,2.1416],[58.7745,10.7079,2.1416,0]};
CoverageArea_max_10 = {[70.1963,19.0363,5.9488,2.1416],[38.5485,8.3284,0,0]};
% For Phi/Psi = +/-40
CoverageArea_mean_40 = {[0,4.5211,2.3795,0],[0,0,0,0]};
CoverageArea_min_40 = {[92.5640,21.1779,6.9007,2.1416],[53.7775,10.4700,1.4277,0]};
CoverageArea_max_40 = {[0,0.4759,0.2380,0],[0,0,0,0]};
for i=1:2
x = [15,30,45,60];
figure
% For Phi/Psi = +/-10
COVERAGE = [CoverageArea_min_10{i};CoverageArea_mean_10{i};CoverageArea_max_10{i}];
COVERAGEAREA = [COVERAGE(:,1)';COVERAGE(:,2)';COVERAGE(:,3)';COVERAGE(:,4)'];
bar(x,COVERAGEAREA);
title({ 'The coverage area' ;[ '\phi = \pm10' , '\psi = \pm10' , 'FEC = 3.8\times10^{-3}' ]});
xlabel( 'Semi-angle at half power, \Phi_1_/_2 (°)' );
ylabel( 'Coverage area (m²)' );
BarNames = { 'min' , 'mean' , 'max' };
legend(BarNames, 'Location' , 'best' );
grid on ;
figure
% For Phi/Psi = +/-40
COVERAGE1 = [CoverageArea_min_40{i};CoverageArea_mean_40{i};CoverageArea_max_40{i}];
COVERAGEAREA1 = [COVERAGE1(:,1)';COVERAGE1(:,2)';COVERAGE1(:,3)';COVERAGE1(:,4)'];
bar(x,COVERAGEAREA1);
title({ 'The coverage area' ;[ '\phi = \pm40' , '\psi = \pm40' , 'FEC = 3.8\times10^{-3}' ]});
xlabel( 'Semi-angle at half power, \Phi_1_/_2 (°)' );
ylabel( 'Coverage area (m²)' );
BarNames = { 'min' , 'mean' , 'max' };
legend(BarNames, 'Location' , 'best' );
grid on ;
end
The problem is all four figures will have the same title. In the above code, each iteration gives me 2 figures. So I need to change the title of every two figures at each new iteration:
Therefore, for the first iteration, I need the following title
title({ 'The coverage area' ;[ '\phi = \pm10' , '\psi = \pm10' , 'FEC = 3.8\times10^{-3}' ]});
For the second iteration, I need the following title
title({ 'The coverage area' ;[ '\phi = \pm10' , '\psi = \pm10' , 'FEC =10^{-5}' ]});
2 Comments
Rasha Shamite
on 19 Dec 2022
Hello,please ,send me massage at my email,''rashasalman19871987@gmail.com''
Answers (1)
VBBV
on 27 Nov 2022
Edited: VBBV
on 27 Nov 2022
close all;
clear variables;
clc;
% % For Phi/Psi = +/-10
CoverageArea_mean_10 = {[84.4735,21.1779,6.4247,2.1416],[45.2112,8.8043,1.1898,0]};
CoverageArea_min_10 = {[98.5128,21.1779,6.9007,2.1416],[58.7745,10.7079,2.1416,0]};
CoverageArea_max_10 = {[70.1963,19.0363,5.9488,2.1416],[38.5485,8.3284,0,0]};
% For Phi/Psi = +/-40
CoverageArea_mean_40 = {[0,4.5211,2.3795,0],[0,0,0,0]};
CoverageArea_min_40 = {[92.5640,21.1779,6.9007,2.1416],[53.7775,10.4700,1.4277,0]};
CoverageArea_max_40 = {[0,0.4759,0.2380,0],[0,0,0,0]};
for i=1:2
x = [15,30,45,60];
figure(i) % use iterator index
subplot(211)
% For Phi/Psi = +/-10
COVERAGE = [CoverageArea_min_10{i};CoverageArea_mean_10{i};CoverageArea_max_10{i}];
COVERAGEAREA = [COVERAGE(:,1)';COVERAGE(:,2)';COVERAGE(:,3)';COVERAGE(:,4)'];
bar(x,COVERAGEAREA);
title({ 'The coverage area' ;[ '\phi = \pm10' , '\psi = \pm10' , 'FEC = 3.8\times10^{-3}' ]});
xlabel( 'Semi-angle at half power, \Phi_1_/_2 (°)' );
ylabel( 'Coverage area (m²)' );
BarNames = { 'min' , 'mean' , 'max' };
legend(BarNames, 'Location' , 'best' );
grid on ;
subplot(212)
% For Phi/Psi = +/-40
COVERAGE1 = [CoverageArea_min_40{i};CoverageArea_mean_40{i};CoverageArea_max_40{i}];
COVERAGEAREA1 = [COVERAGE1(:,1)';COVERAGE1(:,2)';COVERAGE1(:,3)';COVERAGE1(:,4)'];
bar(x,COVERAGEAREA1);
title({ 'The coverage area' ;[ '\phi = \pm40' , '\psi = \pm40' , 'FEC = 3.8\times10^{-3}' ]});
xlabel( 'Semi-angle at half power, \Phi_1_/_2 (°)' );
ylabel( 'Coverage area (m²)' );
BarNames = { 'min' , 'mean' , 'max' };
legend(BarNames, 'Location' , 'best' );
grid on ;
end
4 Comments
VBBV
on 27 Nov 2022
Ok. Then you need to use
if i == 1
figure
....
title({ 'The coverage area' ;[ '\phi = \pm10' , '\psi = \pm10' , 'FEC = 3.8\times10^{-3}' ]});
figure
...
title({ 'The coverage area' ;[ '\phi = \pm10' , '\psi = \pm10' , 'FEC = 3.8\times10^{-3}' ]});
else
figure
...
title({ 'The coverage area' ;[ '\phi = \pm10' , '\psi = \pm10' , 'FEC =10^{-5}' ]});
figure
...
title({ 'The coverage area' ;[ '\phi = \pm10' , '\psi = \pm10' , 'FEC =10^{-5}' ]});
end
Then You need to use and if-else condition inside for loop as above
See Also
Categories
Find more on Graphics Object Programming 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!