Arrays 查找字符数组中存在特定单词的行号

Arrays 查找字符数组中存在特定单词的行号,arrays,matlab,char,Arrays,Matlab,Char,我知道这很简单,我试过Stratch、ismember和cellfun的变体,但没有找到我想要的答案。 我有这样的数据,它们是变量“x”中的日期 2014年12月28日 2014年12月29日 2014年12月30日 2014年12月31日 2015年1月1日 2015年1月2日 2015年1月3日 2015年1月4日 2015年1月5日 我只需要知道哪一行号=='Jan'?因此,分析“x”中的行的结果应该是 结果= 5. 6. 7. 8. 这可能是一种方法- %// Convert to ce

我知道这很简单,我试过Stratch、ismember和cellfun的变体,但没有找到我想要的答案。 我有这样的数据,它们是变量“x”中的日期 2014年12月28日 2014年12月29日 2014年12月30日 2014年12月31日 2015年1月1日 2015年1月2日 2015年1月3日 2015年1月4日 2015年1月5日 我只需要知道哪一行号=='Jan'?因此,分析“x”中的行的结果应该是 结果= 5. 6. 7. 8.
这可能是一种方法-

%// Convert to cell array
x_cell = cellstr(x_cell)

%// Split each cell into cells based on the delimiter "-"
X_split  = cellfun(@(v) strsplit(v,'-'),x_cell,'Uni',0)

%// Look for "Jan" in the second cell of each cell at the "first level"
idx = find(cellfun(@(v) strcmp(v{2},'Jan'),X_split))

使用正则表达式的替代方法:

clear
clc

X = {'28-Dec-2014' ,'29-Dec-2014', '30-Dec-2014', '31-Dec-2014' ,...
    '01-Jan-2015', '02-Jan-2015' ,'03-Jan-2015', '04-Jan-2015', '05-Jan-2015'}

%// Look for any element in X containing Jan
CheckCells = regexp(X,'Jan')

%// Find non-empty cells, resulting from the call to regexp.
Indices = find(~cellfun('isempty',CheckCells))
输出:

Indices =

     5     6     7     8     9

请指定数组
x
。是单元数组还是字符类型的2D矩阵?对不起…实际大小(x)=8797 x 11在我的例子中…Matlab将“x”描述为abc 8797x11字符。在我给出的例子中,x是9 x 11。谢谢,但我不清楚上面例子中x和x的区别。我遇到一个未定义变量的错误。您好-当我尝试在我的变量“x”-->>CheckCells=regexp(x,'Jan')中使用regexp运行示例时,我遇到了这个错误。“STRING”输入必须是一维字符数组或字符串单元格数组。@user2100039您需要使用
cellstr()
在那里可以得到X。谢谢@Divakar+1顺便说一句,我总是忘了在这类任务中使用strsplit和strcmp。还有一件事
cellfun('isempty')
可能更好,因为这是一个内置的。