cellstr
转换为字符向量元胞数组
说明
示例
将字符串数组转换为元胞数组
您可以创建字符串数组来包含多段文本。但是,您可能需要使用接受字符向量元胞数组作为输入参数,但不接受字符串数组的函数。要将字符串数组中的数据传递给此类函数,请使用 cellstr
函数将字符串数组转换为字符向量元胞数组。
创建一个字符串数组。您可以使用双引号创建字符串。
A = ["Past","Present","Future"]
A = 1x3 string
"Past" "Present" "Future"
将字符串数组转换为一个 1×3 的字符向量元胞数组。
C = cellstr(A)
C = 1x3 cell
{'Past'} {'Present'} {'Future'}
将字符数组转换为元胞数组
创建一个字符数组。包括结尾空格以使每行的长度相同,生成一个 3×4 的数组。
A = ['abc ';'defg';'hi ']
A = 3x4 char array
'abc '
'defg'
'hi '
class(A)
ans = 'char'
将字符数组转换为一个 3×1 的字符向量元胞数组。
C = cellstr(A)
C = 3x1 cell
{'abc' }
{'defg'}
{'hi' }
class(C)
ans = 'cell'
将日历持续时间数组转换为元胞数组
创建一个 calendarDuration
数组。
D = calmonths(15:17) + caldays(8) + hours(1.2345)
D = 1x3 calendarDuration
1y 3mo 8d 1h 14m 4.2s 1y 4mo 8d 1h 14m 4.2s 1y 5mo 8d 1h 14m 4.2s
将数组转换为字符向量元胞数组。
C = cellstr(D)
C = 1x3 cell
{'1y 3mo 8d 1h 14...'} {'1y 4mo 8d 1h 14...'} {'1y 5mo 8d 1h 14...'}
class(C)
ans = 'cell'
输入参数
A
— 输入数组
数组
输入数组。A
的数据类型确定 cellstr
如何将 A
转换为字符向量元胞数组。
输入类型 | 转换说明 | 示例输入 | 示例输出 |
---|---|---|---|
| 将每个元素转换为字符向量,并将其赋给某个元胞。 如果 |
1×1 string array "foo" | 1×1 cell array {'foo'} |
1×2 string array "foo" "bar" | 1×2 cell array {'foo'} {'bar'} | ||
字符数组 | 将输入的每行赋给一个元胞。 | 2×3 char array 'foo' 'bar' | 2×1 cell array {'foo'} {'bar'} |
分类数组 | 将输入数组的每个元素转换为一个字符向量,并将该向量赋给新元胞数组中的一个元胞。 | 1x3 categorical array red green blue | 1×3 cell array {'red'} {'green'} {'blue'} |
| 要指定格式和区域设置,请参阅 dateFmt。 |
|
|
dateFmt
— 日期格式和区域设置
字符向量 | 字符串标量
日期格式和区域设置,指定为单独字符向量或字符串标量。A
输入必须是 datetime
、duration
或 calendarDuration
类型。
如果不指定格式,cellstr
将使用 A
的 Format
属性中的值。要仅指定区域设置,请使用空数组作为格式的占位符,即 []
。
示例: cellstr(A, "yyyy-MM-dd")
示例: cellstr(A, "yyyy-MM-dd","en_US")
示例: cellstr(A, [],"en_US")
支持的格式取决于 A
的数据类型。
datetime
格式可以包括单位和分隔符的组合,例如"yyyy-MMM-dd HH:mm:ss.SSS"
。有关详细信息,请参阅datetime
数组的 Format 属性。duration
格式为单个字符(y
、d
、h
、m
或s
)或者以下组合之一:"dd:hh:mm:ss"
"hh:mm:ss"
"mm:ss"
"hh:mm"
以上任意项,用最多九个
S
字符指示秒小数位,例如"hh:mm:ss.SSSS"
calendarDuration
格式可以按从最大到最小时间单位的顺序包括字符y
、q
、m
、w
、d
和t
的组合,例如"ym"
。有关duration
和calendarDuration
格式的详细信息,请参阅设置日期时间显示格式。
区域设置影响用于表示日期和时间的某些分量(例如月份名称)的语言。有效值包括:
"system"
,用于指定您的系统区域设置。xx_YY 形式的字符向量,其中 xx 是用于指定语言的小写 ISO 639-1 双字母代码,YY 是用于指定国家/地区的大写 ISO 3166-1 alpha-2 代码。有关示例值,请参阅
datetime
函数的 Locale 名称-值参数。
扩展功能
tall 数组
对行数太多而无法放入内存的数组进行计算。
此函数完全支持 tall 数组。有关详细信息,请参阅 tall 数组。
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
在生成的代码中,此函数只支持分类数组。有关详细信息,请参阅 Code Generation for Categorical Arrays (MATLAB Coder) 和 Categorical Array Limitations for Code Generation (MATLAB Coder)。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅Run MATLAB Functions in Thread-Based Environment。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
此函数完全支持分布式数组。有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
版本历史记录
在 R2006a 之前推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)