Retrieve strings into a structure wihin a loop

2 visualizaciones (últimos 30 días)
Isma_gp
Isma_gp el 19 de Feb. de 2019
Editada: per isakson el 21 de Feb. de 2019
Hi,
I have a series of folders called "storm_1", "storm_2", etc.
Each folder contains a number of *.dat files named as "NUM_SPEC_1978_6_5_0_0.dat_simo.dat", where the numbers are showing a date that I need to retrieve.
I would like to read through the folders and files and get a structure where I can see the dates of all files contained in each folder. For instance storm_1 folder has 13 files, so I would have 13 dates inside. Something like Structure.storm_1()
I attach a couple of folders as an example.
Can I get some help to retrieve the dates as efficiently as possible?
Thanks
  2 comentarios
Isma_gp
Isma_gp el 21 de Feb. de 2019
a= dir('**/*simo.dat');
for i = 1:length(a)
a(i).name = a(i).name (10:end)
a(i).name = erase(a(i).name, ".dat_simo.dat")
end
dates = []
for i = 1:size(a,1)
mydata = [a(i).name]
[~,d,~] = fileparts(a(i).folder)
dates = setfield(dates,d,'storm_dates',mydata)
end
Isma_gp
Isma_gp el 21 de Feb. de 2019
I'm trying this code, but I only get the first file into each storm within the structure. Can I get some help fixing it? so that all the dates from the same storm are saved into that storm?
Thanks

Iniciar sesión para comentar.

Respuesta aceptada

per isakson
per isakson el 21 de Feb. de 2019
Editada: per isakson el 21 de Feb. de 2019
Haven't used setfield() for a long time. Which release do you use?
Try this
>> Storm = cssm()
Storm =
struct with fields:
storm_1: [1×1 struct]
storm_2: [1×1 struct]
>> Storm.storm_1
ans =
struct with fields:
D_1978_06_05_00: 'NUM_SPEC_1978_6_5_0_0.dat_simo.dat'
D_1978_06_05_03: 'NUM_SPEC_1978_6_5_3_0.dat_simo.dat'
D_1978_06_05_06: 'NUM_SPEC_1978_6_5_6_0.dat_simo.dat'
>>
where
function Storm = cssm()
sad = dir( '**/*simo.dat' );
for file = reshape( sad, 1,[] )
cac = regexp( file.name, '\d[\d_]+', 'match' );
sdn = datenum( cac{:}, 'yyyy_mm_dd_HH_MM' );
str = [ 'D_', datestr( sdn, 'yyyy_mm_dd_HH' ) ];
[ ~, ddd ] = fileparts( file.folder );
Storm.(ddd).(str) = file.name;
end
end
The hours must be included in the field names to make them unique. I added 'D_' because field names must be legal Matlab names.
Wouldn't it be more useful to put the info into a table rather than a struct?

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by