Extracting nth element of nested cell array

22 views (last 30 days)
Isaac
Isaac on 12 Aug 2014
Answered: Azzi Abdelmalek on 12 Aug 2014
I'm trying to extract the nth element of a nested cell array. The nested cell array is of filenames in a directory that are delimited with underscores. I want to extract the 3rd element from each filename and put it into a vector.
Here's my code:
filesInDirectory = dir('InsertDirectoryHere');
filenames = {filesInDirectory.name};
filenameSplit = regexp(filenames, '_', 'split');
The contents of 'filenameSplit' is 33 columns with a 1x5 cell array in each column. I want to extract the 3rd cell in each column and put it into a vector. Here's my attempt:
id = filenameSplit{:}{3};
This does not work, but I can see all elements with the following command (I just don't know how to subset to keep only the 3rd element in each cell array):
filenameSplit{:}
I've read through the forums for similar problems, but I can't find a way to generalize their answers to my problem. I'm new to Matlab so any help would be greatly appreciated.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 12 Aug 2014
filenameSplit=cellfun(@(x) x{3},filenameSplit,'un',0)

Community Treasure Hunt

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

Start Hunting!