MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
character array or a string array. For example, the readtable function provides
the 'TextType' name-value pair argument. This argument specifies whether
readtable returns a table with text in cell arrays of character vectors or string
arrays.

Classes


  • Treat methods as functions.

    • For string adoption, treat methods as though they are functions. Accept string
      arrays as input arguments, and in general, do not change the data type of the
      output arguments, as described in the previous section.



  • Do not change the data types of properties.

    • If a property is a character vector or a cell array of character vectors, then do not
      change its type. When you access such a property, the value that is returned is still
      a character vector or a cell array of character vectors.




As an alternative, you can add a new property that is a string, and make it
dependent on the old property to maintain compatibility.


  • Set properties using string arrays.

    • If you can set a property using a character vector or cell array of character vectors,
      then update your class to set that property using a string array too. However, do
      not change the data type of the property. Instead, convert the input string array to
      the data type of the property, and then set the property.



  • Add a string method.

    • If your class already has a char and/or a cellstr method, then add a string
      method. If you can represent an object of your class as a character vector or cell
      array of character vectors, then represent it as a string array too.




How to Adopt String Arrays in Old APIs


You can adopt strings in old APIs by accepting string arrays as input arguments, and then
converting them to character vectors or cell arrays of character vectors. If you perform
such a conversion at the start of a function, then you do not need to update the rest of it.

The convertStringsToChars function provides a way to process all input arguments,
converting only those arguments that are string arrays. To enable your existing code to

6 Characters and Strings

Free download pdf