MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

load "myVariables.mat"


Error using load
Unable to read file '"myVariables.mat"': Invalid argument.


In command form, double quotes are treated as part of the literal text rather than as the
string construction operator. If you wrote the equivalent of cd "C:\Program Files" in
functional form, then it would look like a call to cd with two arguments.


cd('"C:\Program','Files"')


When specifying arguments as strings, use function syntax. All functions that support
command syntax also support function syntax. For example, you can use cd with function
syntax and input arguments that are double quoted strings.


cd("C:\Program Files")


Why Do Strings in Cell Arrays Return an Error?


When you have multiple strings, store them in a string array, not a cell array. Create a
string array using square brackets, not curly braces. String arrays are more efficient than
cell arrays for storing and manipulating text.


str = ["Venus","Earth","Mars"]


str = 1×3 string array
"Venus" "Earth" "Mars"


Avoid using cell arrays of strings. When you use cell arrays, you give up the performance
advantages that come from using string arrays. And in fact, most functions do not accept
cell arrays of strings as input arguments, options, or values of name-value pairs. For
example, if you specify a cell array of strings as an input argument, then the contains
function throws an error.


C = {"Venus","Earth","Mars"}


C = 1×3 cell array
{["Venus"]} {["Earth"]} {["Mars"]}


TF = contains(C,"Earth")


Error using contains
First argument must be a string array, character vector, or cell array of character vectors.


Instead, specify the argument as a string array.


Frequently Asked Questions About String Arrays
Free download pdf