MatLab 如何循环从一个文件移动到另一个文件?
有没有一种方法可以简单地加载目录中第一个文件的名称而不指定其名称,然后在每次迭代中移至目录中的下一个文件?
我遇到的问题是文件名以 1、1.5、2、2.5、3、3.5 结尾等命名...因此迭代中的 num2str(X) 无助于找到文件。
我试图使用 strrep(s1,s2,s3) 重命名它们,但我再次遇到将它们加载到循环中的相同问题!
我知道我应该一开始就给它们命名,但这些文件太大了,无法再次运行模拟。
这就是我必须重命名文件的内容:
%%%RENAMING A FILE%%%
%Search directory to get number of files
d1=dir('\MATLAB\Data\NumberedQwQoRuns');
numfiles = length(d1)-2;
for a=1:numfiles
%Search subdirectory if necessary for count of those folders
d2=dir('\MATLAB\Data\NumberedQwQoRuns\Run'num2str(a));
subdir = length(d2)-2;
for b= 1:subdir
origname= PROBLEM???
Newname=['Zdata' num2str(b) '.txt']
Newfile= strrep(origname, origname, newname)
movefile(origname,Newfile)
end
end
非常感谢您的帮助, 阿比德A
Is there a way to simply load the name of the first file in a directory without specifying its name, and then in each iteration move on to the next file in the directory?
I have the problem of the file names being named with 1, 1.5, 2,2.5,3, 3.5 endings etc... so num2str(X) in an iteration will not help to locate the file.
I am trying to rename them using strrep(s1,s2,s3), but yet again I run into the same problem of loading them into a loop!
I understand that I should have named them with more planning at first, but these files are much too large to run the simulations again.
This is what I have to rename the files:
%%%RENAMING A FILE%%%
%Search directory to get number of files
d1=dir('\MATLAB\Data\NumberedQwQoRuns');
numfiles = length(d1)-2;
for a=1:numfiles
%Search subdirectory if necessary for count of those folders
d2=dir('\MATLAB\Data\NumberedQwQoRuns\Run'num2str(a));
subdir = length(d2)-2;
for b= 1:subdir
origname= PROBLEM???
Newname=['Zdata' num2str(b) '.txt']
Newfile= strrep(origname, origname, newname)
movefile(origname,Newfile)
end
end
Thank You Very Much for your help,
Abid A
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的解决方案:
我在以下文件结构上测试了它:
Here is my solution:
I tested it on the following file structure:
从
subdir(b).name
获取实际文件名请注意,如果您的合成名称与现有名称之一匹配,则可能会出现问题。
Get the actual filename from
subdir(b).name
Note that you may have problems if your synthesized name matches one of the existing names.