MATLAB Answers

How to induce loop index in the X-label also with changing label name at each iteration?

2 views (last 30 days)
I have two programs saved in a seperate files, for instance a1.m and a2.m, and each program has the following structure. The variable a in the program has two values a=[1,2]; for which I have to run both the programs to get the two outputs for a=1 and a=2. I can apply the loop to run one program twice for a=1 and a=2 but the problem is that I also have to change the x-label name from xlabel1 to xlabel2 of every figure from 1 to 3 at each iteration. And If I apply the loop to this program to run twice, I also have to obtain six figures, three figures for a=1 and three figures for a=2. So I want to apply the loop index i on the whole program so that it runs two times with changing x-label names in each figure at each iteration and gives me two outputs for a=1 and a=2.
%a1.m......
a=1;
for i=length(a)
figure(1)
hold on
.
.
.
.
.
.
.
title('.....')
xlabel('label1');
ylabel('.....');
end
hold on
figure(2)
.
.
.
title('.....')
xlabel('label1');
ylabel('.....');
hold on
figure(3)
.
.
.
title('.....')
xlabel('label1');
ylabel('.....');
%a2.m.......
a=2;
for i=length(a)
figure(1)
hold on
.
.
.
.
.
.
.
title('.....')
xlabel('label2');
ylabel('.....');
end
hold on
figure(2)
.
.
.
title('.....')
xlabel('label2');
ylabel('.....');
hold on
figure(3)
.
.
.
title('.....')
xlabel('label2');
ylabel('.....');

Accepted Answer

Abhishek Gupta
Abhishek Gupta on 12 Apr 2021
Hi,
You can use the sprintf inside the xlabel in the following manner: -
a = [1, 2];
label = {'label1', 'label2'};
for k = 1:size(a,2)
figure;
%%% YOUR CODE %%%
title('.....')
xlabel(sprintf('a%d fig1 %s', a(k), label{k}));
ylabel('.....');
figure;
%%% YOUR CODE %%%
title('.....')
xlabel(sprintf('a%d fig2 %s', a(k), label{k}));
ylabel('.....');
figure;
%%% YOUR CODE %%%
title('.....')
xlabel(sprintf('a%d fig3 %s', a(k), label{k}));
ylabel('.....');
end

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!