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
0 个评论
采纳的回答
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
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!