Print different name than that of the index in figure inside for loop

3 views (last 30 days)
Hello everyone!
I have the next piece of code that gives me a figure of my 4 txt files named e2, e32, e64, e100 for each and every one of the 10 columns that each txt has , hence the for loop goes from 1 to 10 (my txts are 8 rows and 10 columns each).
My problem is that in the title and the name of the saved figure I would like to display a different set of 10 names and not the index i of the for loop .
For example display 10, 100, 200, ....up to 900 and not 1,2,3... ,10
Is there a way to do that?
for i = 1 : 10
figure;
plot(e2(:,i),'LineWidth',2);
hold on;
plot(e32(:,i),'LineWidth',2);
hold on;
plot(e64(:,i),'LineWidth',2);
hold on;
plot(e100(:,i),'LineWidth',2);
hold off;
title(sprintf('%d ', i));
exportgraphics(gca,sprintf('%d.png', i));
end

Accepted Answer

Star Strider
Star Strider on 3 Oct 2022
Probably the easiest way would be to specify a vector for the names and index into it —
v = [10 100:100:900];
for i = 1 : 10
figure;
plot(e2(:,i),'LineWidth',2);
hold on;
plot(e32(:,i),'LineWidth',2);
hold on;
plot(e64(:,i),'LineWidth',2);
hold on;
plot(e100(:,i),'LineWidth',2);
hold off;
title(sprintf('%3d ', v(i)));
exportgraphics(gca,sprintf('%03d.png',v(i)));
end
The '%03d' format descriptor zero-pads the file names with leading zeros for the values with less than 3 digits, making the files eassier to index and sort, if necessary.
.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!