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
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.
답변 (5개)
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
2013년 7월 25일
If the keys should be accepted only an the initial position, add a ^:
idx = regexp(s, '^V[bx]x_')
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
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
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
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
2013년 7월 27일
@Azzi: Do you see why I recommend avoiding anonymous functions in cellfun repeatedly?
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
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
2013년 7월 26일
편집: Andrei Bobrov
2013년 7월 27일
out0 = regexp(s,'^V(b|x)x_\w*','match');
out = cat(1,out0{:});
댓글 수: 4
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!