String Matlab-通过单元格的值查找单元格的索引

String Matlab-通过单元格的值查找单元格的索引,string,matlab,strcmp,cell-array,String,Matlab,Strcmp,Cell Array,我有一个单元格数组filedNames 11x1,其中每个单元格都是一个字符串,我想得到单元格的索引,该索引等于字符串名 我发现了这个例子: C = { {'a'}, {'b'}, {'c'}, {'a'}, {'a'} } % data idx = find(strcmp([C{:}], 'a')) % single line engine 但是,当我将其应用于我的案例时: find(strcmp([fieldNames{:}], 'b_h_epsQ_h')) 没有发生任何事情,strcm

我有一个单元格数组filedNames 11x1,其中每个单元格都是一个字符串,我想得到单元格的索引,该索引等于字符串名

我发现了这个例子:

C = { {'a'}, {'b'}, {'c'}, {'a'}, {'a'} } % data
idx = find(strcmp([C{:}], 'a')) % single line engine
但是,当我将其应用于我的案例时:

find(strcmp([fieldNames{:}], 'b_h_epsQ_h'))
没有发生任何事情,strcmp([fieldNames{:}],'b_h_epsQ_h')本身没有找到匹配项,尽管如果我键入strcmp([fieldNames{2}],'b_h_epsQ_h'),答案是1

我绑定到转置单元阵列,如1x5仍然没有成功的示例所示。请使用以下命令:

idx = strcmp(fieldNames, 'b_h_epsQ_h')
find(idx)
例子: 您也可以尝试:

idx = cellfun(@(x)strcmp(x,'c'), C);
试试这个:

index = zeros(1,length(fieldNames))
for i = i:length(fieldNames)
    if find(strcmp([fieldNames{:}], 'b_h_epsQ_h')) == 1
        index[i] = 1
    end
end

如果要在找到匹配字符串后立即退出,请在'index=i'后面的'If'子句中抛出一个'break'。

该示例是一个包含单元格数组(嵌套)的单元格数组。你的只是一个字符串单元格数组。所以试试:
strcmp(字段名,'b_h_epsQ_h')
index = zeros(1,length(fieldNames))
for i = i:length(fieldNames)
    if find(strcmp([fieldNames{:}], 'b_h_epsQ_h')) == 1
        index[i] = 1
    end
end