MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
Functions


  • Accept any text data types as input arguments.

    • If an input argument can be a string array, then also allow it to be a character
      vector or cell array of character vectors.



  • Accept character arrays as both names and values in name-value pair arguments.

    • In name-value pair arguments, allow names to be specified as either character
      vectors or strings—that is, with either single or double quotes around the name. If
      a value can be a string array, then also allow it to be a character vector or cell
      array of character vectors.



  • Do not accept cell arrays of string arrays for text input arguments.

    • A cell array of string arrays has a string array in each cell. While you can create
      such a cell array, it is not recommended for storing text. If your code uses strings
      as the primary text data type, store multiple pieces of text in a string array, not a
      cell array of string arrays.




However, if your code accepts heterogeneous cell arrays as inputs, then consider
accepting cell arrays that contain strings.


  • In general, return strings.

    • If your function returns output arguments that are text, then return them as string
      arrays.



  • Return the same data type when the function modifies input text.

    • If your function modifies input text and returns the modified text as the output
      argument, then the input and output arguments should have the same data type.




Classes


  • Treat methods as functions.

    • Accept character vectors and cell arrays of character vectors as input arguments,
      as described in the previous section. In general, return strings as outputs.



  • Specify properties as string arrays.

    • If a property contains text, then set the property using a string array. When you
      access the property, return the value as a string array.




6 Characters and Strings

Free download pdf