Create FOR loop to process and create datasets from all files found
Ältere Kommentare anzeigen
Forgetting the need to append all data (which will be my ulimate goal), I need to process all files found to create x number of individual datasets. How do I run the following script with a FOR loop to create these datasets??
% sets base directory and lists all files stored within its subfolders
base_dir = 'Farm34Maize/';
[status, list] = system('dir /B /S TOA5_2436.flux*.dat');
result = textscan(list, '%s', 'delimiter', '\n');
filelist = result{1};
%%Read files
% Run FOR loop to read all .dat files
for i = 1:numel(filelist);
fid = fopen(filelist{i}, 'r');
%%Extract column headers
row = fgetl(fid{i}); % Skip header row
row = fgetl(fid{i}); % Extract second row that represents columns headers
cols = textscan(row, '%q', 'Delimiter', ','); % Identify column headers
cols = strtrim(cols{1}).';
%%Extract data and timestamp
row = fgetl(fid{i}); % Skip third row
row = fgetl(fid{i}); % Skip fourth row
c = length(cols);
format = ['%q', repmat('%f', 1, c-1)];
data = textscan(fid, format, 'Delimiter', ',', 'TreatAsEmpty', {'"NAN"', '"INF"'}, 'CollectOutput', 1, 'headerlines', 1);
%%Split data and timestamp
data = [datenum(data{1}) data{2}]; % In order to work correctly, input file date formats must be 'yyyy-mm-dd HH:MM:SS' or 'yyyy/mm/dd HH:MM:SS PM' or 'y
fclose(fid{i});
end
Thanks in advance.
2 Kommentare
Wieger Duursema
am 9 Jun. 2011
Hi Bugguts99, I am wondering how you did manage to save the data within the FOR LOOP so that they do not overwrite. So, how did you do that? Thanks, Wieger
bugguts99
am 8 Jun. 2012
Akzeptierte Antwort
Weitere Antworten (1)
Jan
am 11 Mai 2011
To learn more about the DIR command:
doc dir
There you find an example of how to scan a specified folder:
dir(fullfile(matlabroot, 'toolbox/matlab/audio/*.m')
Although this does not work recursively, it is much safer.
Kategorien
Mehr zu File Operations finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!