フィルターのクリア

Import files based on file name

12 ビュー (過去 30 日間)
Tessa Kol
Tessa Kol 2020 年 9 月 2 日
編集済み: Stephen23 2020 年 9 月 25 日
I have a folder containing around 300,000 files. I don't need to import all the files.
Problem: How can I import the files based on specific file name?
Problem example:
In the picture below I have a section of the files, which are all in the same folder. I only want to import the .data files. But I don't need all the .data files to be import only the last one of every serie.
Up until now I have the following code:
files = dir(fullimpfile(pwd,'*.data*'));
expData = cell(length(files),1);
for i = 1:length(files)
fid = fopen(fullfile(files(i).folder,files(i).name),'r');
%% Reading the data
% Read all the data from the file
dataRead = textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f','HeaderLines',1);
end
This code imports all of the data files. How can I use I think a for loop to only import only the last .data files of every serie?

採用された回答

Stephen23
Stephen23 2020 年 9 月 2 日
fnm = {...
'LedgeTest_muSP_0.10_muRP_0.10.1.data.0',...
'LedgeTest_muSP_0.10_muRP_0.10.1.data.1',...
'LedgeTest_muSP_0.10_muRP_0.10.1.data.2',...
'LedgeTest_muSP_0.10_muRP_0.10.1.data.21',...
'LedgeTest_muSP_0.10_muRP_0.20.1.data.0',...
'LedgeTest_muSP_0.10_muRP_0.20.1.data.1',...
'LedgeTest_muSP_0.10_muRP_0.20.1.data.2',...
'LedgeTest_muSP_0.10_muRP_0.20.1.data.11'}
spl = regexp(fnm,'\.data\.','split','once');
spl = vertcat(spl{:});
vec = str2double(spl(:,2));
[~,idx] = sort(vec);
[~,idy,idz] = unique(spl(idx,1),'last');
out = fnm(idx(idy))
Giving:
out =
'LedgeTest_muSP_0.10_muRP_0.10.1.data.21'
'LedgeTest_muSP_0.10_muRP_0.20.1.data.11'
Use it like this:
D = pwd;
tmp = dir(fullfile(D,'*.data.*'));
fnm = {tmp.name};
...
for k = 1:numel(out)
fid = fopen(fullfile(D,out{k}),'r');
...
end
  9 件のコメント
Tessa Kol
Tessa Kol 2020 年 9 月 25 日
files(idx(idy)).folder
Did not work either. I got an error saying:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in LedgeTest2D_results (line 33)
dataRead = textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f','HeaderLines',1);
Stephen23
Stephen23 2020 年 9 月 25 日
編集済み: Stephen23 2020 年 9 月 25 日
It would probably be easier to get rid of out altogether and just sort the structure itself, e.g.:
files = files(idx(idy));
and then inside the loop you can simply access the folder and name, e.g.:
for k = 1:nume(files)
fnm = fullfile(files(k).folder,files(k).folder);
fid = fopen(fnm,'rt');
...
end

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by