Extracting nth element of nested cell array

8 Ansichten (letzte 30 Tage)
Isaac
Isaac am 12 Aug. 2014
Beantwortet: Azzi Abdelmalek am 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.

Antworten (1)

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

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by