Shehroz
Shehroz

Reputation: 447

MATLAB: Finding files within a directory with specific names?

If I have files with multiple names and I want to select only few from them, how can I do that in MATLAB. For e.g. I have the following file names in a directory

1adl.txt, 2adl.txt...

1adlKey.txt, 2adlKey.txt ...

where *adl.txt are the files containing data and *Key.txt are the files containing 'keys' to extract useful information from a*.txt

The problem is when I use

files = dir(fullfile(newdir,'*.txt') );

it gives me all the .txt files, whereas I want to read a*.txt and a*Key.txt separately, so I can do a one-to-one correspondence between them. Can regular expression be used here? If so, then how?

Any help will be appreciated.

Upvotes: 2

Views: 1445

Answers (2)

brodoll
brodoll

Reputation: 1881

I'd do the following to read them separately:

nokeyfiles = dir(fullfile(newdir,'?a?[^Key].txt'));
keyfiles = dir(fullfile(newdir,'*Key.txt'));

Upvotes: 2

See the following code line.

files = dir(fullfile(newdir,'*adl*.txt') );

Pls visit Matlab Documentation for more information on dir command.

Upvotes: 0

Related Questions