How to find specifying pattern in the string

1 次查看(过去 30 天)
i have many variables like Vbx_fg_ih_kj,Vbxfg_ih_kj, Vxx_gf_ij_kl, Vamhji_ many more. i want to search variable who are in the format of Vbx_, and Vxx_, rest i want to ignore. How to do this . Pls help me
one thing i thought of strcmpi function but they are so many variable that is i have to write every new line for each variable, how to do this in single. pls help me.
  1 个评论
Abhishek  Trivedi
Abhishek Trivedi 2013-7-27
thank you for your answer i already using loop But my main pblm variable van be in only in pattern V**_, * can be any charcter . I want genralise it.I do not want to put every character there is in strncmpli. so help me finding a way so i serch for variable whose starting in pattern this pattern V**_. thank you again for answer

请先登录,再进行评论。

回答(5 个)

Evan
Evan 2013-7-25
It sounds like regexp would do what you're wanting.
idx = regexp(s,'V[bx]x_')
If idx returns anything other than empty. You have a match. You could then apply this to all your variable names in a loop or using cellfun, depending on how they're arranged.
For more:
help regexp
doc regexp
  1 个评论
Jan
Jan 2013-7-25
If the keys should be accepted only an the initial position, add a ^:
idx = regexp(s, '^V[bx]x_')

请先登录,再进行评论。


Jan
Jan 2013-7-25
s = {'Vbx_fg_ih_kj','Vbxfg_ih_kj', 'Vxx_gf_ij_kl', 'Vamhji_ many','Vbxx_'};
match = s(strncmp(s, 'Vbx_', 4) | strncmp(s, 'Vxx_', 4));
While regexp is very powerful, the simple strncmp is much faster when the matching matters the initial part of he string.
  4 个评论
Jan
Jan 2013-7-26
编辑:Jan 2013-7-26
@Cedric: In CELLFUN it is not a function name, which is passed to the string, but the string is compared and the processing happens directly in the Mex function. One example is the command 'prodofsize', which is not a function but acts like numel.
Unfortunately cellfun.c is not shipped with modern Matlab versions, but is was in e.g. Matlab 6.5. Reading the C-file it get obvious, that the determination of the longest dimension directly avoids the overhead of mexCallMATLAB or mxFevalFunctionHandle.
Cedric
Cedric 2013-7-26
Ok, I was quite wrong about the internals then; I thought that JIT was doing something comparable to passing str2func('isempty') to CELLFUN. Thank you for the explanation!

请先登录,再进行评论。


Jan
Jan 2013-7-26
Some timings under R2009a/Win7/64:
s = {'Vbx_fg_ih_kj','Vbxfg_ih_kj', 'Vxx_gf_ij_kl', 'Vamhji_ many','Vbxx_'};
s = repmat(s, 1, 1000);
tic; m = s(cellfun(@(x) ~isempty(regexp(x,'V(b|x)x_')),s)); toc;
% Elapsed time is 0.205530 seconds.
tic; m = s(~cellfun('isempty', regexp(s,'V(b|x)x_'))); toc;
% Elapsed time is 0.015089 seconds.
tic;
out0 = regexp(s,'^V(b|x)x_\w*','match');
m = cat(1,out0{:});
toc;
% Elapsed time is 0.017384 seconds.
tic; m = s(strncmp(s, 'Vbx_', 4) | strncmp(s, 'Vxx_', 4)); toc
% Elapsed time is 0.000420 seconds.
  1 个评论
Jan
Jan 2013-7-27
@Azzi: Do you see why I recommend avoiding anonymous functions in cellfun repeatedly?

请先登录,再进行评论。


Azzi Abdelmalek
Azzi Abdelmalek 2013-7-25
s={'Vbx_fg_ih_kj','Vbxfg_ih_kj', 'Vxx_gf_ij_kl', 'Vamhji_ many','Vbxx_'}
idx=s(cellfun(@(x) ~isempty(regexp(x,'V(b|x)x_')),s))
  1 个评论
Jan
Jan 2013-7-25
编辑:Jan 2013-7-25
This is faster, which might matter if larger data sets are processed:
idx = s(~cellfun('isempty', regexp(s,'V(b|x)x_')))
For 50 elements this is 5.5 time faster, but we are talking about milliseconds only. But for a {1 x 5000} cell string, the time goes from 0.2 to 0.008 seconds. cellfun is slow, when it is used with anonymous functions and I strongly recommend to avoid it, when it is possible - and here it is very easy to avoid it.

请先登录,再进行评论。


Andrei Bobrov
Andrei Bobrov 2013-7-26
编辑:Andrei Bobrov 2013-7-27
out0 = regexp(s,'^V(b|x)x_\w*','match');
out = cat(1,out0{:});
  4 个评论
Jan
Jan 2013-7-27
@Andrei: Would it be more friendly not to post a comment but to fix the typo directly? I hesitate to edit other ones postings.
Andrei Bobrov
Andrei Bobrov 2013-7-27
Jan, as you comfortably, in any case, I would be grateful.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by