且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MATLAB搜索单元格数组以获取字符串子集

更新时间:2023-11-15 15:59:58

要做的是将这个想法封装为一个函数.要么内联:

The thing to do is to encapsulate this idea as a function. Either inline:

substrmatch = @(x,y) ~cellfun(@isempty,strfind(y,x))

findmatching = @(x,y) y(substrmatch(x,y))

或包含在两个m文件中:

Or contained in two m-files:

function idx = substrmatch(word,cellarray)
    idx = ~cellfun(@isempty,strfind(word,cellarray))

function newcell = findmatching(word,oldcell)
    newcell = oldcell(substrmatch(word,oldcell))

所以现在您只需输入

>> findmatching('words',cellArray)
ans = 
    'nicewords'    'morewords'