selecting file from a database (.txt files) ?

조회 수: 1 (최근 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 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