Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How to find specifying pattern in the string

Asked by Abhishek Trivedi on 25 Jul 2013

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 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

Abhishek  Trivedi


No products are associated with this question.

5 Answers

Answer by 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 Simon on 25 Jul 2013

If the keys should be accepted only an the initial position, add a ^:

idx = regexp(s, '^V[bx]x_')
Answer by Jan Simon 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.


Evan on 25 Jul 2013

Unfortunately, the number of functions for which cellfun is optimized in this manner is small, so I think a lot of us get into the habit of assuming our only option is to pass a function handle. Always good to have this reminder.

Jan Simon 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 Wannaz 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!

Jan Simon
Answer by Jan Simon 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.
out0 = regexp(s,'^V(b|x)x_\w*','match');
m = cat(1,out0{:});
% 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 Simon on 27 Jul 2013

@Azzi: Do you see why I recommend avoiding anonymous functions in cellfun repeatedly?

Jan Simon
Answer by 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 Simon 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.

Azzi Abdelmalek
Answer by Andrei Bobrov on 26 Jul 2013
Edited by Andrei Bobrov on 27 Jul 2013
out0 = regexp(s,'^V(b|x)x_\w*','match');
out = cat(1,out0{:});


Andrei Bobrov on 27 Jul 2013

Thank you Jan, corrected.

Jan Simon 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 on 27 Jul 2013

Jan, as you comfortably, in any case, I would be grateful.

Andrei Bobrov

Contact us