Print different name than that of the index in figure inside for loop
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
lena kappa
am 3 Okt. 2022
Kommentiert: Star Strider
am 3 Okt. 2022
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
0 Kommentare
Akzeptierte Antwort
Star Strider
am 3 Okt. 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!