strfind用法 规定第二个参数类型只能是向量 不可以是cell 。
Syntax
start = regexp(str,expr)
[start,finish] = regexp(str,expr)
[start,finish,tokens] = regexp(str,expr)
[...] = regexp(str,expr,'once')
Description
start = regexp(str,expr)返回一个向量,元素分别是表达式expr在字符串Str中的位置。表达式可以是:'c[aeiou]+t' (cat,caet, caoueouat)
当Str或者expr 是个元胞字符数组,regexp 返回 m-by-n元胞数组,其元素是向量,其中M是Str该字符串的指数,
数组里面的字符串轮流查找。
1从一组字符串里面,
string1={'42_time[ ]_', 'speed_vel', '002_air', 'water_005', 'att059[ ]', '895dps'}
查找下面几个字符串,
string0={'time', 'air','water'}
并且给出它们所在的列数。
*time 返回1
*air 返回3
*water 返回4
唉 看到cell就知道又是麻烦....
string1={'42_time[ ]_', 'speed_vel', '002_air', 'water_005', 'att059[ ]', '895dps'}
string0={'time', 'air','water'};
for i=1:size(string0,2)
s=regexp(string1,string0(i));
weizhi=find(cellfun('isempty',s)==0);
disp([weizhi string1(weizhi)])
end
[1] '42_time[ ]_'
[3] '002_air'
[4] 'water_005'
string1={'42_time[ ]_' ,'speed_vel' ,'002_air', 'water_005','att059[ ]','895dps'}
string0={'time','air','water'}
string1={'42_time[ ]_' ,'speed_vel' ,'002_air', 'water_005','att059[ ]','895dps'};
string0={'time', 'air','water'};
for i=1:size(string0,2)
s=strfind(string1,string0{i});
weizhi=find(cellfun('isempty',s)==0);
disp([weizhi string1(weizhi)])
end
注意string0{i}
结果一致
[1] '42_time[ ]_'
[3] '002_air'
[4] 'water_005'
也可以用匿名函数
string1={'42_time[ ]_' ,'speed_vel' ,'002_air', 'water_005','att059[ ]','895dps'};
string0={'time', 'air','water'};
f=@(x)find(~cellfun(@isempty,strfind(string1,x{:})));
arrayfun(f,string0)
ans = 1 3 4
稍作修改:
string1={'42_time[ ]_' ,'speed_vel' ,'002_air', 'water_005','att059[ ]','895dps'};
string0={'time', 'air','water'};
f=@(x)find(~cellfun(@isempty,strfind(string1,x))); % 将x{:}直接修改为x
cellfun(f,string0) % 此时需要将arrayfun修改为cellfun
arrayfun和cellfun果然是强大啊