MatLab 如何循环从一个文件移动到另一个文件?

发布于 2024-12-11 05:15:54 字数 754 浏览 0 评论 0原文

有没有一种方法可以简单地加载目录中第一个文件的名称而不指定其名称,然后在每次迭代中移至目录中的下一个文件?

我遇到的问题是文件名以 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 技术交流群。

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

发布评论

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

评论(2

左耳近心 2024-12-18 05:15:54

这是我的解决方案:

%# get runs subdirectories
BASE_DIR = '/path/to/Runs';
runsDir = dir( fullfile(BASE_DIR,'Runs') );
runsDir = {runsDir([runsDir.isdir]).name};           %# keep only directory names
runsDir = runsDir( ~ismember(runsDir, {'.' '..'}) ); %# ignore "." and ".."

for r=1:numel(runsDir)
    %# get files in subdirectory
    runFiles = dir(fullfile(BASE_DIR,'Runs',runsDir{r},'*.txt')); %# *.txt files
    runFiles = {runFiles.name};                                   %# file names

    %# map filenames: 1,1.5,2,2.5,... into 1,2,3,4,...
    [~,ord] = sort(str2double( regexprep(runFiles,'\.txt

我在以下文件结构上测试了它:

Runs/
|
|__Run1/
|  |__1.txt        will become: Zdata_1.txt
|  |__1.5.txt                   Zdata_2.txt
|  |__2.txt                     Zdata_3.txt
|  |__2.5.txt                   etc...
|  |__3.txt
|  |__3.5.txt
|
|__Run2/
   |__1.txt
   |__1.5.txt
   |__2.txt
   |__2.5.txt
   |__3.txt
   |__3.5.txt
,'') )); newrunFiles = cellstr( num2str(ord(:),'Zdata_%d.txt') ); newrunFiles = strtrim(newrunFiles); %# rename files for f=1:numel(runFiles) fname = fullfile(BASE_DIR,'Runs',runsDir{r},runFiles{f}); fnameNew = fullfile(BASE_DIR,'Runs',runsDir{r},newrunFiles{f}); movefile(fname,fnameNew); end end

我在以下文件结构上测试了它:

Here is my solution:

%# get runs subdirectories
BASE_DIR = '/path/to/Runs';
runsDir = dir( fullfile(BASE_DIR,'Runs') );
runsDir = {runsDir([runsDir.isdir]).name};           %# keep only directory names
runsDir = runsDir( ~ismember(runsDir, {'.' '..'}) ); %# ignore "." and ".."

for r=1:numel(runsDir)
    %# get files in subdirectory
    runFiles = dir(fullfile(BASE_DIR,'Runs',runsDir{r},'*.txt')); %# *.txt files
    runFiles = {runFiles.name};                                   %# file names

    %# map filenames: 1,1.5,2,2.5,... into 1,2,3,4,...
    [~,ord] = sort(str2double( regexprep(runFiles,'\.txt

I tested it on the following file structure:

Runs/
|
|__Run1/
|  |__1.txt        will become: Zdata_1.txt
|  |__1.5.txt                   Zdata_2.txt
|  |__2.txt                     Zdata_3.txt
|  |__2.5.txt                   etc...
|  |__3.txt
|  |__3.5.txt
|
|__Run2/
   |__1.txt
   |__1.5.txt
   |__2.txt
   |__2.5.txt
   |__3.txt
   |__3.5.txt
,'') )); newrunFiles = cellstr( num2str(ord(:),'Zdata_%d.txt') ); newrunFiles = strtrim(newrunFiles); %# rename files for f=1:numel(runFiles) fname = fullfile(BASE_DIR,'Runs',runsDir{r},runFiles{f}); fnameNew = fullfile(BASE_DIR,'Runs',runsDir{r},newrunFiles{f}); movefile(fname,fnameNew); end end

I tested it on the following file structure:

浪菊怪哟 2024-12-18 05:15:54

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.

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