我必须在这个循环的开头添加什么?

发布于 2024-08-22 20:24:15 字数 300 浏览 4 评论 0原文

如何使用 for 循环读取以下文件:(循环可以忽略文件名中的字符吗?)

abc-1.TXT
cde-2.TXT
Ser-3.TXT
wsz-4.TXT
aqz-5.TXT
iop-6.TXT

我必须在此循环的开头添加什么?

for i = 1:1:6  
    nom_fichier = strcat(['MyFile\.......' num2str(i) '.TXT']);

how I can read the following files using the for loop: (can the loop ignore the characters in filenames?)

abc-1.TXT
cde-2.TXT
ser-3.TXT
wsz-4.TXT
aqz-5.TXT
iop-6.TXT

What do I have to add at the beginning of this loop ??

for i = 1:1:6  
    nom_fichier = strcat(['MyFile\.......' num2str(i) '.TXT']);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在夏天 2024-08-29 20:24:15

您可以使用 DIR 来避免构建文件名命令。例如:

myfiles = dir('*.txt');
for i = 1:length(myfiles)
    nom_fichier = myfiles(i).name;
    ...do processing here...
end

You can avoid constructing the filenames by using the DIR command. For instance:

myfiles = dir('*.txt');
for i = 1:length(myfiles)
    nom_fichier = myfiles(i).name;
    ...do processing here...
end
少钕鈤記 2024-08-29 20:24:15

首先,为什么要在这里使用 strcat ?这本身就是一个单字符串。所有连接均已通过方括号 [] 完成。

['MyFile\.......' num2str(i) '.TXT']

接下来,我不确定你在这里问什么问题。是如何加载数据的吗?如果文件只是分隔的数字,每行的数量相同,则 load 足以加载它们,或者您可能需要文本读取。

我的猜测是您不知道如何构建文件名的主要部分。您可以这样做:

Names = {'abc' 'cde 'ser' 'wsz' 'aqz' 'iop'};
for i = 1:6
  fn = ['MyFile',filesep,Names{i},'-',num2str(i),'.TXT'];
  data = load(fn);

  % do other stuff ...

end

如果您不想通过输入名称来创建变量,则可以使用 dir,也许像这样创建文本文件名列表:

Names = dir('MyFile\*.TXT');

First of all, why would you use strcat here? This is, by itself, a SINGLE string. All concatenation has already been done by the brackets [].

['MyFile\.......' num2str(i) '.TXT']

Next, I'm not certain what is your question here. Is it how to load in the data? If the files are simply delimited numbers, with the same number of them on each line, then load will suffice to load them in, or perhaps you may need textread.

My guess is you do not know how to build the main part of of the file name. You might do it this way:

Names = {'abc' 'cde 'ser' 'wsz' 'aqz' 'iop'};
for i = 1:6
  fn = ['MyFile',filesep,Names{i},'-',num2str(i),'.TXT'];
  data = load(fn);

  % do other stuff ...

end

If you don't want to create a variable with the names by typing them in, then use dir, perhaps like this to create a list of text file names:

Names = dir('MyFile\*.TXT');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文