在已排序文件的文件夹中查找特定文件

时间:2019-03-04 08:46:18

标签: matlab

我有一个包含不同文件扩展名的文件夹,例如.txt,.xls。

文件的命名顺序如下:

  • 20190228_0_20_20
  • 20190228_50_20_50

我整理了所有扩展名为.txt的文件。在此排序的文件夹中,我想对名称为20190218_0_20_20的特定文件进行细化并加载它,然后进行一些计算。这是我的代码。

非常感谢:

%processing the parent folder

myfolder ='C:\Users\yannick\Desktop\Windkanal_Data\Yannick';

if~isdir(myfolder)
    Error_message =sprintf('Error,Folder not found :\n %s',myfolder);
end

%Getting list of all files with  file pattern .txt

filepattern =fullfile(myfolder,'*.txt');

txtfiles=dir(filepattern);

%sorting out the file with name  20190228_0_20_20 .txt 

1 个答案:

答案 0 :(得分:-1)

嗯,我们不得不大胆猜测您在这里想要做什么,但我会给您一个机会:

% dir only those that match your format
filepattern =fullfile(myfolder,'20190228_0_*_*.txt'); 
% get all matching files
txtfiles=dir(filepattern);
% let's loop them
for iFile = 1:length(txtfiles)
    % extracting the x and y from the file name of the current file
    fn_parts = split(f(iFile).name,{'_','.'});
    coor_x = str2double(fn_parts{3});
    coor_y = str2double(fn_parts{4});
    % now you know the coordinates (x,y) that belong to the current file. You can use this to search for a specific coordinate if you like (though it would be easier to do this earlier I guess)
    % next step is reading it I guess. I have no idea about your file format. Try dlmread? importdata?
    fcontents = importdata(fullfile(f(iFile).folder,f(iFile).name)); 
    % process further...

end