For Loop - need to iterate values at the same time.

1 回表示 (過去 30 日間)
Michael Vaccaro
Michael Vaccaro 2021 年 8 月 27 日
コメント済み: Michael Vaccaro 2021 年 8 月 27 日
Hi there! I am trying to set up for loops that iterate two values at the same time in order to retrieve files with 2 changing variables. My current setup is to do the following (I am trying to use a nested for loop since I know that you cannot do a loop with 2 variables in one):
for x = [1:10]
fstart = 'D:\Michael Files\Data\Run ';
f2 = sprintf('%05d', x);
f3 = '\Ascii\Xxsv00001 ';
for y = [267:277]
fend = sprintf('%03dX.txt', y);
end
fname = [fstart f2 f3 fend];
disp(fname)
end
D:\Michael Files\Data\Run 00001\Ascii\Xxsv00001 277X.txt D:\Michael Files\Data\Run 00002\Ascii\Xxsv00001 277X.txt D:\Michael Files\Data\Run 00003\Ascii\Xxsv00001 277X.txt D:\Michael Files\Data\Run 00004\Ascii\Xxsv00001 277X.txt D:\Michael Files\Data\Run 00005\Ascii\Xxsv00001 277X.txt D:\Michael Files\Data\Run 00006\Ascii\Xxsv00001 277X.txt D:\Michael Files\Data\Run 00007\Ascii\Xxsv00001 277X.txt D:\Michael Files\Data\Run 00008\Ascii\Xxsv00001 277X.txt D:\Michael Files\Data\Run 00009\Ascii\Xxsv00001 277X.txt D:\Michael Files\Data\Run 00010\Ascii\Xxsv00001 277X.txt
In the end, this is the output that I get from Matlab. The Run numbers iterate correctly, but I ultimately need the last number to iterate as well:
D:\Michael Files\Data\Run 00001\Ascii\Xxsv00001 267X.txt
...
D:\Michael Files\Data\Run 00010\Ascii\Xxsv00001 277X.txt
If anyone knows a good way to do this I'd really appreciate the help! Thank you so much!!

採用された回答

Kevin Holly
Kevin Holly 2021 年 8 月 27 日
for x = [1:10]
fstart = 'D:\Michael Files\Data\Run ';
f2 = sprintf('%05d', x);
f3 = '\Ascii\Xxsv00001 ';
y = 266+x;
fend = sprintf('%03dX.txt', y);
fname = [fstart f2 f3 fend];
disp(fname)
end
  2 件のコメント
Michael Vaccaro
Michael Vaccaro 2021 年 8 月 27 日
Thank you so much! I was overthinking it!
Stephen23
Stephen23 2021 年 8 月 27 日
Note that FULLFILE is recommended (rather than concatenating strings together):

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

その他の回答 (1 件)

the cyclist
the cyclist 2021 年 8 月 27 日
Even easier:
for x = 1:10
fname = sprintf('D:\\Michael Files\\Data\\Run %05d\\Ascii\\Xxsv00001 %03dX.txt',x,x+266);
disp(fname)
end

カテゴリ

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

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by