Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slparse_assignment
Home > sltoolbox > text > slparse_assignment.m

slparse_assignment

PURPOSE ^

SLPARSE_ASSIGNMENT Parses an assignment string

SYNOPSIS ^

function [name, value] = slparse_assignment(str)

DESCRIPTION ^

SLPARSE_ASSIGNMENT Parses an assignment string

 $ Syntax $
   - [name, value] = slparse_assignment(str)

 $ Arguments $
   - str:          the string representing an assignment
   - name:         the name of the value
   - value:        the value

 $ Description $
   - [name, value] = slparse_assignment(str) parses an assignment string
     and extracts the name and value. The string should be in the form
     of <name> = <value>. For the syntax of assignment, we have following
     rules:
       - spaces are allowed between <name> and = and between = and <value>
       - <name> should be a valid Matlab name, which should be checked
         by isvarname
       - The form of <value> can be either of the following:
           - a numeric scalar (will be converted to double)
           - a matrix as expressed in matlab (will be converted to matrix)
           - a string (will be converted to char string)
           - a string quoted by ' or " (will be de-quoted)

 $ Remarks $
   - The whole string can have multiple =, but only the first one will
     be considered as the assignment mark.

 $ History $
   - Created by Dahua Lin, on Aug 9th, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
This function is called by:
  • edl_readenvvars EDL_READENVVARS Reads in a file with environment variables

SOURCE CODE ^

0001 function [name, value] = slparse_assignment(str)
0002 %SLPARSE_ASSIGNMENT Parses an assignment string
0003 %
0004 % $ Syntax $
0005 %   - [name, value] = slparse_assignment(str)
0006 %
0007 % $ Arguments $
0008 %   - str:          the string representing an assignment
0009 %   - name:         the name of the value
0010 %   - value:        the value
0011 %
0012 % $ Description $
0013 %   - [name, value] = slparse_assignment(str) parses an assignment string
0014 %     and extracts the name and value. The string should be in the form
0015 %     of <name> = <value>. For the syntax of assignment, we have following
0016 %     rules:
0017 %       - spaces are allowed between <name> and = and between = and <value>
0018 %       - <name> should be a valid Matlab name, which should be checked
0019 %         by isvarname
0020 %       - The form of <value> can be either of the following:
0021 %           - a numeric scalar (will be converted to double)
0022 %           - a matrix as expressed in matlab (will be converted to matrix)
0023 %           - a string (will be converted to char string)
0024 %           - a string quoted by ' or " (will be de-quoted)
0025 %
0026 % $ Remarks $
0027 %   - The whole string can have multiple =, but only the first one will
0028 %     be considered as the assignment mark.
0029 %
0030 % $ History $
0031 %   - Created by Dahua Lin, on Aug 9th, 2006
0032 %
0033 
0034 %% parse and verify input arguments
0035 
0036 if ~ischar(str)
0037     error('sltoolbox:invalidarg', ...
0038         'The str should be a char string');
0039 end
0040 
0041 str = strtrim(str);
0042 name = [];
0043 value = [];
0044 if isempty(str)
0045     return;
0046 end
0047 
0048 
0049 %% divide parts
0050 
0051 peq = find(str == '=', 1);
0052 if isempty(peq)
0053     error('sltoolbox:parseerror', ...
0054         'Fail to locate the assignment equal mark');
0055 end
0056 
0057 name = strtrim(str(1:peq-1));
0058 value = strtrim(str(peq+1:end));
0059 
0060 %% check name
0061 
0062 if isempty(name) || ~isvarname(name)
0063     error('sltoolbox:parseerror', ...
0064         'The name is invalid for assignment in %s', str);
0065 end
0066 
0067 %% process value
0068 
0069 if isempty(value)
0070     value = [];
0071     return;
0072 end
0073 
0074 trynums = str2num(value);
0075 if ~isempty(trynums)
0076     value = trynums;
0077 elseif length(value) >= 2 && ...
0078         ((value(1) == '''' && value(end) == '''') || (value(1) == '"' && value(end) == '"'))
0079     value = value(2:end-1);
0080     if isempty(value)
0081         value = '';
0082     end
0083 end
0084

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com