Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何检查特定变量的字符串值是否存在于数组中?我知道strcmp并且ismember是选项,但我将如何调整这些选项,以便它们使用变量的值在数组中搜索,而不是我输入要搜索的字符串。所以我的代码看起来像:
strcmp
ismember
C1 = {'red' 'yellow'}; C2 = {'green' 'blue'}; fn = 'blue'; %Comparison function here if % fn is present in C1 c = 'm'
谢谢
我不明白有什么问题strcmp,因此我不确定我是否理解你的问题。考虑这个
if any( strcmp(fn,C2) ) disp('OK!') % // OR c = 'm' end OK!
How about
if any( cellfun( @(x) isequal(x, fn), C1 ) ) c = 'm'; end
I believe you may also use regexp.
regexp