vartype
将变量类型作为下标对表或时间表进行索引
说明
示例
在表中选择变量
创建包含数值和字符串变量的表。随后通过下标对该表进行索引以仅获取其数值变量。
LastName = ["Smith";"Johnson";"Williams";"Jones";"Brown"]; Age = [38;43;38;40;49]; Height = [71;69;64;67;64]; Weight = [176;163;131;133;119]; BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80]; T = table(LastName,Age,Height,Weight,BloodPressure)
T=5×5 table
LastName Age Height Weight BloodPressure
__________ ___ ______ ______ _____________
"Smith" 38 71 176 124 93
"Johnson" 43 69 163 109 77
"Williams" 38 64 131 125 83
"Jones" 40 67 133 117 75
"Brown" 49 64 119 122 80
使用 vartype
函数创建下标。通过下标对 T
的第二个维度进行索引,以返回仅包含数值变量的表。
S = vartype('numeric');
T2 = T(:,S)
T2=5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
38 71 176 124 93
43 69 163 109 77
38 64 131 125 83
40 67 133 117 75
49 64 119 122 80
可以为 isa
函数接受的任何类型创建下标。从 T
中选择字符串变量。
S = vartype('string');
T3 = T(:,S)
T3=5×1 table
LastName
__________
"Smith"
"Johnson"
"Williams"
"Jones"
"Brown"
在时间表中选择变量
创建包含数值、字符串和分类变量的时间表。随后通过下标对该表进行索引以仅获取其数值变量。
Date = datetime(["12/18/2015";"12/19/2015";"12/20/2015"]); Temp = [45;33;36]; Pressure = [30.1;29.3;29.7]; Location = ["Boston";"Boston";"Worcester"]; SensorType = categorical(["S1";"X7";"S1"]); TT = timetable(Date,Temp,Pressure,Location,SensorType)
TT=3×4 timetable
Date Temp Pressure Location SensorType
___________ ____ ________ ___________ __________
18-Dec-2015 45 30.1 "Boston" S1
19-Dec-2015 33 29.3 "Boston" X7
20-Dec-2015 36 29.7 "Worcester" S1
使用 vartype
函数创建下标。通过下标对 TT
的第二个维度进行索引,以返回仅包含数值变量的时间表。TT2
还具有来自 TT
的行时间,因为这些时间标识行。行时间向量是时间表的一个属性,而不是其变量之一。
S = vartype('numeric');
TT2 = TT(:,S)
TT2=3×2 timetable
Date Temp Pressure
___________ ____ ________
18-Dec-2015 45 30.1
19-Dec-2015 33 29.3
20-Dec-2015 36 29.7
输入参数
扩展功能
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
版本历史记录
在 R2016b 中推出
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)