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

3 ビュー (過去 30 日間)
lena kappa
lena kappa 2022 年 10 月 3 日
コメント済み: Star Strider 2022 年 10 月 3 日
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

採用された回答

Star Strider
Star Strider 2022 年 10 月 3 日
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 件のコメント
lena kappa
lena kappa 2022 年 10 月 3 日
That's so simple and clever! Thank you @Star Strider!!!
Star Strider
Star Strider 2022 年 10 月 3 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTitle についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by