Create FOR loop to process and create datasets from all files found
Show older comments
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 Comments
Wieger Duursema
on 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
on 8 Jun 2012
Accepted Answer
More Answers (1)
Jan
on 11 May 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.
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!