strc

PURPOSE ^

STRC Convert Matlab/OCTAVE char array to C string array.

SYNOPSIS ^

function s = strc(c)

DESCRIPTION ^

STRC  Convert Matlab/OCTAVE char array to C string array.

  Syntax:
    S = STRC(C)

  Description:
    S = STRC(C) converts the blank padded char array C to a new char array S
    with the same strings in its rows but as C strings (null terminated):
      - always add a null character at the end of the string, and
      - replace all trailing whitespace characters with null characters.
    C may also be a string cell array and is converted to char array with CHAR.

  Notes:
    The output char array S is always one column bigger than the input one C
    (because function CHAR pads the shorter rows in a cell array up to the
    length of the longest one).

  Examples:
    c = {'- What''s that?'; '  ...  '; ''; '  ...  '; '- Surprise!!!'}
    s = strc(c)
    a = char(c)
    uint8(a)
    uint8(strc(c))

  See also:
    CHAR
    CELLSTR

  Authors:
    Joan Pau Beltran  <joanpau.beltran@socib.cat>

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

strc.m

SOURCE CODE ^

0001 function s = strc(c)
0002 %STRC  Convert Matlab/OCTAVE char array to C string array.
0003 %
0004 %  Syntax:
0005 %    S = STRC(C)
0006 %
0007 %  Description:
0008 %    S = STRC(C) converts the blank padded char array C to a new char array S
0009 %    with the same strings in its rows but as C strings (null terminated):
0010 %      - always add a null character at the end of the string, and
0011 %      - replace all trailing whitespace characters with null characters.
0012 %    C may also be a string cell array and is converted to char array with CHAR.
0013 %
0014 %  Notes:
0015 %    The output char array S is always one column bigger than the input one C
0016 %    (because function CHAR pads the shorter rows in a cell array up to the
0017 %    length of the longest one).
0018 %
0019 %  Examples:
0020 %    c = {'- What''s that?'; '  ...  '; ''; '  ...  '; '- Surprise!!!'}
0021 %    s = strc(c)
0022 %    a = char(c)
0023 %    uint8(a)
0024 %    uint8(strc(c))
0025 %
0026 %  See also:
0027 %    CHAR
0028 %    CELLSTR
0029 %
0030 %  Authors:
0031 %    Joan Pau Beltran  <joanpau.beltran@socib.cat>
0032 
0033 %  Copyright (C) 2014-2016
0034 %  ICTS SOCIB - Servei d'observacio i prediccio costaner de les Illes Balears
0035 %  <http://www.socib.es>
0036 %
0037 %  This program is free software: you can redistribute it and/or modify
0038 %  it under the terms of the GNU General Public License as published by
0039 %  the Free Software Foundation, either version 3 of the License, or
0040 %  (at your option) any later version.
0041 %
0042 %  This program is distributed in the hope that it will be useful,
0043 %  but WITHOUT ANY WARRANTY; without even the implied warranty of
0044 %  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0045 %  GNU General Public License for more details.
0046 %
0047 %  You should have received a copy of the GNU General Public License
0048 %  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0049   
0050   c = char(c);
0051   [m n] = size(c);
0052   s = char(ones(m, n+1, 'uint8') * 32);
0053   s(1:m, 1:n) = c;
0054   [v, i] = min(isspace(s(:,end:-1:1)), [], 2);
0055   z = bsxfun( @gt, 1:n+1, n - i + 2);
0056   z(v,:) = true;
0057   s(z) = 0;
0058 
0059 end

Generated on Fri 06-Oct-2017 10:47:42 by m2html © 2005