How to find specifying pattern in the string

2 views (last 30 days)
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 Comment
Abhishek  Trivedi
Abhishek Trivedi on 27 Jul 2013
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

Sign in to comment.

Answers (5)

Evan
Evan on 25 Jul 2013
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 Comment
Jan
Jan on 25 Jul 2013
If the keys should be accepted only an the initial position, add a ^:
idx = regexp(s, '^V[bx]x_')

Sign in to comment.


Jan
Jan on 25 Jul 2013
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 Comments
Jan
Jan on 26 Jul 2013
Edited: Jan on 26 Jul 2013
@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 on 26 Jul 2013
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!

Sign in to comment.


Jan
Jan on 26 Jul 2013
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 Comment
Jan
Jan on 27 Jul 2013
@Azzi: Do you see why I recommend avoiding anonymous functions in cellfun repeatedly?

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 25 Jul 2013
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 Comment
Jan
Jan on 25 Jul 2013
Edited: Jan on 25 Jul 2013
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.

Sign in to comment.


Andrei Bobrov
Andrei Bobrov on 26 Jul 2013
Edited: Andrei Bobrov on 27 Jul 2013
out0 = regexp(s,'^V(b|x)x_\w*','match');
out = cat(1,out0{:});
  4 Comments
Jan
Jan on 27 Jul 2013
@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 on 27 Jul 2013
Jan, as you comfortably, in any case, I would be grateful.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!