selecting file from a database (.txt files) ?

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by