kalmanfans's Blog

模式识别 非线性预测 复杂网络 Following your heart

Matlab 函数之strfind, regexp

kalmanfans posted @ 2012年2月07日 13:35 in Matlab with tags Matlab 函数 strfind regexp , 7282 阅读

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该字符串的指数,

http://www.weizmann.ac.il/matlab/techdoc/ref/regexp.html

 

数组里面的字符串轮流查找。


1从一组字符串里面,
string1={'42_time[ ]_', 'speed_vel', '002_air', 'water_005', 'att059[ ]', '895dps'}
查找下面几个字符串,
string0={'time', 'air','water'}
并且给出它们所在的列数。
*time   返回1
*air   返回3
*water  返回4

 

唉 看到cell就知道又是麻烦....

  1. string1={'42_time[ ]_', 'speed_vel', '002_air', 'water_005', 'att059[ ]', '895dps'}
    string0={'time', 'air','water'};
  2. for i=1:size(string0,2)
  3. s=regexp(string1,string0(i));
  4. weizhi=find(cellfun('isempty',s)==0);
  5. disp([weizhi  string1(weizhi)])
  6. end

   [1]    '42_time[ ]_'

   [3]    '002_air'

   [4]    'water_005'

 

  1. string1={'42_time[ ]_'  ,'speed_vel' ,'002_air', 'water_005','att059[ ]','895dps'}
  2. string0={'time','air','water'}
  3. string1={'42_time[ ]_'  ,'speed_vel' ,'002_air',  'water_005','att059[ ]','895dps'};
  4. string0={'time', 'air','water'};
  5. for i=1:size(string0,2)
  6. s=strfind(string1,string0{i});
  7. weizhi=find(cellfun('isempty',s)==0);
  8. disp([weizhi  string1(weizhi)])
  9. end

注意string0{i}
结果一致

  1.     [1]    '42_time[ ]_'
  2.  
  3.     [3]    '002_air'
  4.  
  5.     [4]    'water_005'

也可以用匿名函数

  1. string1={'42_time[ ]_'  ,'speed_vel' ,'002_air', 'water_005','att059[ ]','895dps'};
  2. string0={'time', 'air','water'};
  3. f=@(x)find(~cellfun(@isempty,strfind(string1,x{:})));
  4. arrayfun(f,string0)

ans =     1     3     4

稍作修改:

  1. string1={'42_time[ ]_'  ,'speed_vel' ,'002_air', 'water_005','att059[ ]','895dps'};
  2. string0={'time', 'air','water'};
  3. f=@(x)find(~cellfun(@isempty,strfind(string1,x))); % 将x{:}直接修改为x
  4. cellfun(f,string0) % 此时需要将arrayfun修改为cellfun

arrayfuncellfun果然是强大啊
 

 

Avatar_small
seo service london 说:
2024年1月16日 14:35

Great! We will be connecting to this enormous post on our site. Continue the good writing.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter