Retrieve strings into a structure wihin a loop

조회 수: 2 (최근 30일)
Isma_gp
Isma_gp 2019년 2월 19일
편집: per isakson 2019년 2월 21일
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에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by