Indexing Cell Array created by RegExp, sorting file names

2 views (last 30 days)
I've created a cell array of the names of certain files in a selected folder using the following
files = dir('*.dat');
names = {files.name};
Now that I have my file names stored, I want to sort them by their names. Firstly I want to identify the file names of the sort, 'resting_ABC_1.dat'
So I used regexp to find these names and then filled the empty [] sections of the cell array using the following
getrest = regexp(names,'resting_ABC_(\d*).dat','tokens');
emptyIndex = cellfun('isempty',getrest);
getrest(emptyIndex) = {0};
I don't have much experience with cell arrays, and I'm having trouble identifying the row, column of the cell array not equal to zero. I could then use these numbers to extract the name from my names cell array and process only the data fitting my specifications. I want to be able to apply this code to any folder and have it find and process the data with my file name specifications. Any ideas would be greatly appreciated

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 15 Nov 2013
Edited: Azzi Abdelmalek on 15 Nov 2013
Maybe you want
getrest = regexp(names,'resting_ABC_\d+.dat','match');
files=cellfun(@(x) x{1},getrest(~cellfun('isempty',getrest)),'un',0)
  1 Comment
Leigh
Leigh on 15 Nov 2013
That works! Thank you! I'm also working on using regexp to locate the files in the folder that have the following format:
L_somename_2.dat R_somename_2.dat
The characteristics are as follows: 1. There will always be a capital L or R 2. The number will vary 3. The underscore after L and R as well as before the number is required 4. The number of characters in 'somename' will change
Some file names include:
L_chart_3.dat R_set_4.dat

Sign in to comment.

Categories

Find more on File Operations 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!