Extracting strings from cell of strings--and can you use logical vectors with strings?
4 次查看(过去 30 天)
显示 更早的评论
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?
1 个评论
Azzi Abdelmalek
2014-2-15
编辑:Azzi Abdelmalek
2014-2-15
I think it's easier for us, if you just explain what you want to get
采纳的回答
Jan
2014-2-15
Perhaps you want somtheing like this:
allnames = {excelfiles.name};
len = cellfun('prodofsize', allnames);
match = or(len == 17, len == 18);
matchnames = allnames(match);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!