Import and read ascii files located in different folders

3 visualizzazioni (ultimi 30 giorni)
Hello to all! I need to import and read in Matlab a great number of files organized in a hierarchic structure of folders such as
G:\MET_MM\trans_YYYYMMDD00\val1_.txt
G:\MET_MM\trans_YYYYMMDD00\val2*.txt
....
G:\MET_MM\trans_YYYYMMDD00\val8*.txt
where YYYY stands for year MM for monthy and DD for day
For a given value of MM I have 29 to 30 subfolders trans_YYYYMMDD00 depending on the number of days in the given month
In each of the folders trans_YYYYMMDD00 I have 8 groups of ascii files In each group the filenames are formed as follows
val1_yyyymmdd_HH00.txt
where HH stands for the hour (HH = 24 to 48)
I need to import for an entire month each one of the groups of files val1*.txt to val8*.txt to read the values and to obtain the corresponding variables VAL1, VAL2, VAL3,... containing the values of the parameter val1, val2,... for the entire month
I have tried the following solution: ============================================================================
month = 11;
% Import parameter DEW POINT
TD = [];
j = 14;
while j <= 29
PATH_TD = ['G:\MET_' int2str(month) '\trans_2011' int2str(month) int2str(j) '00_txt\val_dew2011' int2str(luna) int2str(j) '_*.txt'];
names = dir(PATH_TD);
names = {names.name};
for k = 1:25
fid = getfield(names,{1,k});
fid_txt = char(fid);
M = load(fid_txt);
TD = [TD; M];
end
eval(['save TD_' int2str(month) '_' int2str(j) ' TD'])
j = j+1
end
============================================================================
It works only once (for the first j = 14) and its stops with the message
??? Error using ==> load
Unable to read file val_dew20111115_2400.txt: No such file or directory.
I have noticed that PATH_TD has correctly changed and also the filenames in name are correct ============================================================================ Can you give me an idea ? Thanks a lot for your time
Ileana
  1 Commento
Oleg Komarov
Oleg Komarov il 8 Ago 2012
Do not eval files!
Think about it, 8 files per day, 30 day per month, 12 month per year... You really wanna have 365*8 files in your workspace?
Create ONE structure with fieldnames.

Accedi per commentare.

Risposte (1)

Oleg Komarov
Oleg Komarov il 8 Ago 2012
Modificato: Oleg Komarov il 8 Ago 2012
You are creating the
PATH_TD = ['G:\MET_' int2str(month) '\trans_2011' int2str(month) int2str(j) '00_txt\val_dew2011' int2str(luna) int2str(j) '_*.txt'];
manually.
Use dir instead
s = dir(sprintf('G:\MET_%d',month));
idx = [s(:).isdir];
subs = {s(idx).name}';
% Remove . and ..
subs = setdiff(subs,{'.','..'});
Now subs has all the subfolders of G:\MET_%d.
Loop for each subfolder and import the files, again I suggest to dir each subfolder in case a file is missing.
% Loop for each subfolder
for ii = 1:numel(subs)
s = dir(subs{ii});
idx = [s(:).isdir];
files = {s(~idx).name}';
% Import files
for jj = 1:numel(files)
out.(subs{ii}).(files{jj}) = load(files{jj});
end
end

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