MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Frequently Asked Questions About String Arrays


MATLAB introduced the string data type in R2016b. Starting in R2018b, you can use
string arrays to work with text throughout MathWorks products. String arrays store
pieces of text and provide a set of functions for working with text as data. You can index
into, reshape, and concatenate strings arrays just as you can with arrays of any other
type. For more information, see “Create String Arrays” on page 6-12.

In most respects, strings arrays behave like character vectors and cell arrays of character
vectors. However, there are a few key differences between string arrays and character
arrays that can lead to results you might not expect. For each of these differences, there
is a recommended way to use strings that leads to the expected result.

Why Does Using Command Form With Strings Return An
Error?

When you use functions such as the cd, dir, copyfile, or load functions in command
form, avoid using double quotes. In command form, arguments enclosed in double quotes
can result in errors. To specify arguments as strings, use functional form.

With command syntax, you separate inputs with spaces rather than commas, and you do
not enclose input arguments in parentheses. For example, you can use the cd function
with command syntax to change folders.

cd C:\Temp

The text C:\Temp is a character vector. In command form, all arguments are always
character vectors. If you have an argument, such as a folder name, that contains spaces,
then specify it as one input argument by enclosing it in single quotes.

cd 'C:\Program Files'

But if you specify the argument using double quotes, then cd throws an error.

cd "C:\Program Files"

Error using cd
Too many input arguments.

The error message can vary depending on the function that you use and the arguments
that you specify. For example, if you use the load function with command syntax and
specify the argument using double quotes, then load throws a different error.

6 Characters and Strings

Free download pdf