Extracting strings from cell of strings--and can you use logical vectors with strings?

2 views (last 30 days)
Hi all, I'm trying to extract data from a structure. I've succeeded in pulling out what I want--which is a cell array of strings (of names of files). What I want to do next is choose a subset of these file names which are of a certain length (my analyses don't apply to some of the excel files in my WD). I am trying to do this by creating a logical vector corresponding to 'file = correct length?' and multiplying it by the cell of strings, but I get the error
"??? Undefined function or method 'times' for input arguments of type 'cell'."
I have included more code than is necessary so you can see what I'm trying to do. So far I've pulled out the data this way:
excelfiles=dir([DataDir '/*.xls']); %creates structure of excel files from my data folder
allnames={excelfiles.name}; %creates cell of strings pulled from "name" within "excelfiles"
******[problem]*******"all names" is a cell of strings; see below************
%selecting only a subset of the excel files, to choose by length of file name
FI=1:length(excelfiles);
names={excelfiles(FI).name}; %pulling names one at a time
namesindex=1:length(names);
currentname=names{namesindex};
nameslogic18(namesindex)=logical(length(currentname)==18); %logical for names of length 18
nameslogic17(namesindex)=logical(length(currentname)==17); %logical for names of length 17
nameslogic=nameslogic18+nameslogic17; %sum of both logicals
nameslogic=logical(nameslogic); %converting from double to logical
durexcelfiles=nameslogic.*names'; %want to pull out just those names that are correct length
I have tried to convert 'allnames' using char, but that returns a 28x21 char that appears to be all the file names (28 of them) concatenated into one cell. I know it's technically not one cell by looking at the dimensions of the output, but nonetheless I'm trying to get a 28x1 or 1x28 array of strings that can be multiplied by a logical. I also tried with the downloaded m file "cell2str" but that returns a similar char file of 1x616.
If this is not possible, how should I proceed? Can I somehow take the element #s (positions) of "1"s in my logical, and use that to pull out the corresponding entries in my "allnames" cell array?

Accepted Answer

Jan
Jan on 15 Feb 2014
Perhaps you want somtheing like this:
allnames = {excelfiles.name};
len = cellfun('prodofsize', allnames);
match = or(len == 17, len == 18);
matchnames = allnames(match);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!