Cycling through file names in a struct

17 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 5 Jan 2021
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});

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!