フィルターのクリア

selecting file from a database (.txt files) ?

3 ビュー (過去 30 日間)
ludvikjahn
ludvikjahn 2015 年 2 月 20 日
コメント済み: Elias Gule 2015 年 2 月 20 日
Good Morning, I have a series of data, say in a folder, which are saved with this name: "NAME_NUMBEROFMACHINE_YYMMDD_HHmmSS" (YY= years, HH= hours, etc.) and I'd like to import to my workspace only the data of a specific day, or of a specific machine. Is there a way to do it easily in Matlab or should I use another programme? Thanks Gabriele

採用された回答

Elias Gule
Elias Gule 2015 年 2 月 20 日
function files = listFiles(key,directory,ext)
%%LIST FILES
% key : the machine name or day
% : if searching for day , use a numeric value
% : and use a string when searching for machine name.
% directory : the full path of the search folder.
% ext : the desired file extension.
% The name search is case-insensitive : regexpi was used instead of regexp
%
% NB: The code can surely be improved: This was just a quick solution.
switch nargin
case 1
directory = '';
ext = '*';
case 2
ext = '*';
otherwise
% DO NOTHING
end
if(isnumeric(key))
isDaySearch = true;
else
isDaySearch = false;
end
if(isDaySearch)
pattern = sprintf('.*%s_%s%02d_%s','\w+\d+','\d{4}',key,'\d{6}');
else
pattern = sprintf('.*%s%s',key,'\w+');
end
[status files] = fileattrib(fullfile(directory,ext));
if ~isempty(files)
files = {files(1:end).Name};
files = cellfun(@(x) char(regexpi(x,pattern,'match')),files,...
'UniformOutput',false);
files = files(~cellfun('isempty',files));
else
files = cell(1,1);
end
end
  1 件のコメント
Elias Gule
Elias Gule 2015 年 2 月 20 日
This function will output a cell array of files matching the key. You can then use the function "importdata" to load data from each file; if the files contain only numeric data then you can use the "dlmread" function which will save each file data in one matrix.

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

その他の回答 (1 件)

Elias Gule
Elias Gule 2015 年 2 月 20 日
編集済み: Elias Gule 2015 年 2 月 20 日
Please see attached file.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by