MATLAB: Is there any way to do a regex to exclude filenames with underscore from datastore

MATLAB

I have a directory with lot of subfolders, and each folder containing HEA and DAT files.
For example, a '30001' folder directory would contain he following files:
30001.hea
30001_0001.dat
30001_000.hea
30001_0002.dat
30001_0002.hea
30001_0017.dat
30001_0017.hea
30001n.dat
30001n.hea
RECORDS
Furthermore, the 'RECORDS' file in '30001' directory contains the following information:
30001
30001n
30001_0001
30001_0002
30001_0003
0001_0017
Is there any way to do a regex to exclude anything with underscore from datastore?

Best Answer

Datastore does not support regex directly.
A potential workaround that might also be to use "*" in location specified to datastore:
>> tds = tabularTextDatastore([data_dir, filesep, '*', filesep, 'RECORDS');
This gives you a tabularTextDatastore with only the RECORDS files. You can now read in these files one-by-one and build up the actual Files property:
>> keepFiles = [];
>> while hasdata(ttds)
>> R = read(ttds);
>> keepFiles = [keepFiles; R(~contains(R.Var1,'_layout.hea') & ~contains(R.Var1,'.hea'),:)];
>> end
Now, you have just the files you need:
>> fds = fileDatastore(keepFiles, 'ReadFcn', @some_custom_read_fcn);