Changeset 6614


Ignore:
Timestamp:
11/17/10 15:48:02 (14 years ago)
Author:
jschierm
Message:

str2int.m: Added capability to find last as well as first integer in character string.

Location:
issm/trunk/src/m/utils/Array
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk/src/m/utils/Array/find_string.m

    r33 r6614  
    11%
    22%  function to find a string in a cell array
     3%  (actually pretty close to matlab strmatch)
    34
    45%  [ifound]=find_string(cells,str)
  • issm/trunk/src/m/utils/Array/str2int.m

    r4027 r6614  
    11%
    2 %  function to find and read the first integer in a character string.
     2%  function to find and read the first or last positive integer
     3%  in a character string.
    34%
    4 %  function [aint]=str2int(astr)
     5%  function [aint]=str2int(astr,cfl)
    56%
    6 function [aint]=str2int(astr);
     7function [aint]=str2int(astr,cfl);
    78
    89aint=[];
    910
    10 i=1;
    11 while (i <= length(astr))
    12         if (astr(i) >= '0' && astr(i) <= '9')
    13                 aint=sscanf(astr(i:length(astr)),'%d',[1,1]);
    14                 break
    15         else
    16                 i=i+1;
     11if     ~exist('cfl','var') || strncmpi(cfl,'f',1)
     12    i=1;
     13
     14    while (i <= length(astr))
     15        if (astr(i) >= '0' && astr(i) <= '9')
     16            aint=sscanf(astr(i:length(astr)),'%d',[1,1]);
     17            return
     18        else
     19            i=i+1;
     20        end
    1721        end
     22
     23elseif strncmpi(cfl,'l',1)
     24    i=length(astr);
     25    ifound=false;
     26
     27    while (i >= 1)
     28        if     (astr(i) >= '0' && astr(i) <= '9')
     29            ifound=true;
     30            i=i-1;
     31        elseif ~ifound
     32            i=i-1;
     33        else
     34            aint=sscanf(astr(i+1:length(astr)),'%d',[1,1]);
     35            return
     36        end
     37        end
     38
     39    if ifound
     40        aint=sscanf(astr,'%d',[1,1]);
     41        return
     42    end
    1843end
    1944
Note: See TracChangeset for help on using the changeset viewer.