octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: strmatch Matlab compatibility


From: Ben Abbott
Subject: Re: strmatch Matlab compatibility
Date: Sun, 07 Aug 2011 11:40:39 -0400

On Aug 7, 2011, at 10:38 AM, Rik wrote:

> 8/7/11
> 
> As part of my ongoing quest to remove unnecessarily slow cellfun functions
> I came across one in strmatch where I need some compatibility information
> from Matlab.  When strmatch compares strings with the 'exact' option it
> ignores spaces at the end of the line.  The question is are they ignoring
> just spaces or whitespace as well.  Can someone run the following?
> 
> strmatch ("a", {"a ", "a\0", "a\t", "a\r", "a\n", "a\v"}, 'exact')
> 
> strmatch ("a ", ["a"], 'exact')
> 
> strmatch ("a\0", ["a"], 'exact')
> 
> strmatch ("a\t", ["a"], 'exact')
> 
> Cheers,
> Rik

Rik,

I'm running ML version ...

>> version

ans =

7.11.0.584 (R2010b)

The double quote syntax you provided above is not supported by ML. Using single 
quotes in ML gives the same behavior as single quotes in Octave. Thus, 'a\t' is 
3 characters, and sprintf('a\t') is two characters. Unfortunately, 
sprintf('a\0') is one character, so I was a bit more creative. Thus, to get the 
intended result, I used ...

>> strmatch ('a', {'a ', ['a',char(0)], sprintf('a\t'), sprintf('a\r'), 
>> sprintf('a\n'), sprintf('a\v')}, 'exact')

ans =

     2

>> strmatch ('a ', ['a'], 'exact')

ans =

     []

>> strmatch (['a',char(0)], ['a'], 'exact')

ans =

     []

>> strmatch (sprintf('a\t'), ['a'], 'exact')

ans =

     []

Notice that when using "a/0" the order of the arguments to strmatch matters.

>> strmatch ('a', ['a',char(0)], 'exact')

ans =

     1

Unless I don't understand how strmatch is intended to work, it looks like the 
documentation for ML is wrong or there is a but in their implementation. 
Consider the simple examples below.

>> strmatch ('a', 'a')

ans =

     1

>> strmatch ('a', 'a', 'exact')

ans =

     1

>> strmatch ('a ', 'a')

ans =

     []

>> strmatch ('a ', 'a', 'exact')

ans =

     []

>> strmatch ('a', 'a ')

ans =

     1

>> strmatch ('a', 'a ', 'exact')

ans =

     1

>From the doc-string, I'd expect all to return [1].

 STRMATCH Find possible matches for string.
    I = STRMATCH(STR, STRARRAY) looks through the rows of the character
    array or cell array of strings STRARRAY to find strings that begin
    with the string contained in STR, and returns the matching row indices. 
    Any trailing space characters in STR or STRARRAY are ignored when 
    matching. STRMATCH is fastest when STRARRAY is a character array. 
 
    I = STRMATCH(STR, STRARRAY, 'exact') compares STR with each row of
    STRARRAY, looking for an exact match of the entire strings. Any 
    trailing space characters in STR or STRARRAY are ignored when matching.
 
    Examples
      i = strmatch('max',strvcat('max','minimax','maximum'))
    returns i = [1; 3] since rows 1 and 3 begin with 'max', and
      i = strmatch('max',strvcat('max','minimax','maximum'),'exact')
    returns i = 1, since only row 1 matches 'max' exactly.
    
    STRMATCH will be removed in a future release. Use STRNCMP instead.

Ben


reply via email to

[Prev in Thread] Current Thread [Next in Thread]