Extracting nth element of nested cell array

조회 수: 8 (최근 30일)
Isaac
Isaac 2014년 8월 12일
답변: Azzi Abdelmalek 2014년 8월 12일
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.

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 12일
filenameSplit=cellfun(@(x) x{3},filenameSplit,'un',0)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by