Cycling through file names in a struct

조회 수: 7 (최근 30일)
Nat Person
Nat Person 2021년 1월 4일
답변: Walter Roberson 2021년 1월 5일
I have an nx1 struct which contains the names of all the files in the directory Im using. For context, here's an example 6x1 struct:
'677_FM02_HF_6-16-2020.ep' 'C:\Users\Desktop\F-DAS Matlab\ProcessFiles' '04-Jan-2021 13:49:41' 10644 false 738160.576168982
'685_FM01_HF_7-21-2020.ep' 'C:\Users\Desktop\F-DAS Matlab\ProcessFiles' '04-Jan-2021 13:49:41' 10619 false 738160.576168982
'689_FM03_HF_6-16-2020.ep' 'C:\Users\Desktop\F-DAS Matlab\ProcessFiles' '04-Jan-2021 13:49:41' 10644 false 738160.576168982
'699_FM04_HF_.6-16-2020.ep' 'C:\Users\Desktop\F-DAS Matlab\ProcessFiles' '04-Jan-2021 13:49:41' 10642 false 738160.576168982
'766_FM06_HF_6-15-2020.ep' 'C:\Users\Desktop\F-DAS Matlab\ProcessFiles' '04-Jan-2021 13:49:41' 10625 false 738160.576168982
'786_FM05_HF_6-15-2020.ep' 'C:\Users\Desktop\F-DAS Matlab\ProcessFiles' '04-Jan-2021 13:49:41' 10919 false 738160.576168982
I want to cycle through the first column (the name column) in a for loop where each loop saves the file name as a variable.
So far the code I have is
FMdir = dir('*FM*.ep'); %Get a list of all of the final measurement files in the directory
FMn = numel(FMdir);
for n= 1:FMn %cycle through
if( isnumeric(FMdir.(fn{n})))
end
But it's throwing an error saying
Error using isnumeric
Too many input arguments.
Error in ProcessFilesTry (line 19)
if( isnumeric(FMdir.(fn{n})))
end
Anyone know how I can fix this?

채택된 답변

Walter Roberson
Walter Roberson 2021년 1월 5일
FMdir is a struct array with fields such as name and folder . You would not want to dynamically construct references to the fields of that struct.
FMdir = dir('*FM*.ep'); %Get a list of all of the final measurement files in the directory
FMn = numel(FMdir);
for n = 1 : FMn
thisname = FMdir(n).name;
[~, basename, ext] = fileparts(thisname);
as_num = str2double(basename);
if ~isnan(as_num)
%filename was purely numeric
end
end
However... not even one of the filenames you post is purely numeric. Did you want to extract up to the first underscore and test that to see if it is numeric?
nameparts = regexp(basename, '_', 'split');
as_num = str2double(nameparts{1});

추가 답변 (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