在数组中搜索变量

时间:2013-01-05 15:41:51

标签: matlab matrix

如何检查数组中是否存在特定变量的字符串值?我知道strcmpismember是选项,但我如何调整它们以便它们使用变量的值来搜索数组,而不是输入我想要搜索的字符串。所以我的代码看起来像是:

C1 = {'red' 'yellow'};
C2 = {'green' 'blue'};
fn = 'blue';  

%Comparison function here

if % fn is present in C1
    c = 'm'

由于

2 个答案:

答案 0 :(得分:4)

我不明白strcmp有什么问题,所以我不确定我理解你的问题。考虑一下这个

if any( strcmp(fn,C2) ) 
   disp('OK!')    % // OR  c = 'm'
end


OK!

答案 1 :(得分:2)

怎么样

if any( cellfun( @(x) isequal(x, fn), C1 ) )
   c = 'm';
end

我相信您也可以使用regexp

相关问题