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

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Star Strider
Star Strider le 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.
.
  2 commentaires
lena kappa
lena kappa le 3 Oct 2022
That's so simple and clever! Thank you @Star Strider!!!
Star Strider
Star Strider le 3 Oct 2022
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by