Retrieve strings into a structure wihin a loop

1 次查看(过去 30 天)
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 个评论
Isma_gp
Isma_gp 2019-2-21
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 2019-2-21
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

请先登录,再进行评论。

采纳的回答

per isakson
per isakson 2019-2-21
编辑:per isakson 2019-2-21
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?

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by