selecting file from a database (.txt files) ?

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Elias Gule
Elias Gule il 20 Feb 2015
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 Commento
Elias Gule
Elias Gule il 20 Feb 2015
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.

Accedi per commentare.

Più risposte (1)

Elias Gule
Elias Gule il 20 Feb 2015
Modificato: Elias Gule il 20 Feb 2015
Please see attached file.

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by