5.0

5.0 | 3 ratings Rate this file 8 Downloads (last 30 days) File Size: 2.83 KB File ID: #1518
image thumbnail

strpat: a pedestrian, exactly matching pattern finder / replacer

by us

 

20 Mar 2002 (Updated 22 Apr 2010)

STRPAT finds and replaces an exactly matching pattern in a character or numeric string

| Watch this File

File Information
Description

STRPAT searches for EXACTLY matching patterns
in a character or numeric string and replaces
the occurrences with another pattern.

a single or double precision pattern
may include NaNs

% examples

% - character strings

% note difference
% STRREP
strrep('aa_a_aaa_aa','a','XXX')
% XXXXXX_XXX_XXXXXXXXX_XXXXXX
%
% STRPAT
strpat('aa_a_aaa_aa','a','XXX')
% aa_XXX_aaa_aa

strpat('aa_a_aaa_aa','aa','X')
% X_a_aaa_X

% - numeric strings
strpat([1:3,pi,5:7],pi,nan)
% 1 2 3 NaN 5 6 7

strpat(pi*(1:6),pi*(4:5),[])
% 3.1416 6.2832 9.4248 18.85

strpat(pi*(1:6),pi*(4:5),[nan inf -inf nan])
% 3.1416 6.2832 9.4248 NaN Inf -Inf NaN 18.85

strpat(pi*(1:6),pi*(4:5),1:3)
% 3.1416 6.2832 9.4248 1 2 3 18.85

% - PATTERN including NaNs
strpat([-10,nan,1,-10,nan,nan,1,nan,1,-10],[nan,1],inf)
% -10 Inf -10 NaN Inf Inf -10

see also
>> help strpat
or the screenshot for help on this self-explanatory function

MATLAB release MATLAB 6.1 (R12.1)
Other requirements optimized for R2010a+
Tags for This File  
Everyone's Tags
array, exact, find, match(2), numeric, pattern, regexp, regexprep, replace, string, strings, strrep
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
26 Feb 2011 mathworks2011

excellent. overcomes the short comings of regexprep

19 Apr 2008 Carlos Adrian Vargas Aguilera

I downloaded it because I found it at 2nd place at "Most downloaded"!, and I found it very useful. I just would prefer a WARNING message instead of a DISPLAY, in order to be able to avoid it and just looking into the .n field at the second output, which, by the way, was very clever.

Thanks us for the submission.

06 Sep 2007 Leandro Navarro  
Updates
18 Feb 2003

simply changed submission name!

18 Feb 2003

adjusted use of LOGICAL to new def in R13

19 May 2003

enhanced help

21 May 2003

enhanced help

03 Jan 2006

returns an error message if it does not find an exactly matching pattern; version tag 30-Dec-2005 18:44:23

05 Jul 2006

changed engine for r14/r2006 versions, which runs much faster (no EVAL!). the old version has been added as STRPAT13. version tag 05-Jul-2006 16:12:19

19 Apr 2010

a pattern now may include NaNs; version tag 18-Apr-2010 02:33:59

22 Apr 2010

finally removed all pre-r12.1 legacy code for logical indexing; correction of a typo; version tag 22-Apr-2010 09:57:37

Contact us