2

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.

Benoit_11
  • 13,905
  • 2
  • 24
  • 35
Shehroz
  • 347
  • 2
  • 9
  • 25

2 Answers2

2

I'd do the following to read them separately:

nokeyfiles = dir(fullfile(newdir,'?a?[^Key].txt'));
keyfiles = dir(fullfile(newdir,'*Key.txt'));
brodoll
  • 1,851
  • 5
  • 22
  • 25
0

See the following code line.

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

Pls visit Matlab Documentation for more information on dir command.

Huá dé ní 華得尼
  • 1,248
  • 1
  • 18
  • 33