matlab常⽤函数——数据类型函数
三、基本数据类型函数
1)数值函数
double:转换为双精度浮点数
single:转换为单精度浮点数
typecast:在不改变数据⼤⼩情况下转换数据类型  Y=typecast(X,type)
type可选范围:'uint8'、'int8'、'uint16'、'int16'、'uint32'、'int32'、'uint64'、'int64'、'single'、'double'
arrayfun:把函数应⽤到数组中的每个元素 A=arrayfun(fun, S)应⽤fun函数到数组S中的每个元素,将结果返回给数组A。
A=arrayfun(fun, S, T, …)
实例:
1. x(1).f1=2;x(2).f1=3;
2. x(1).f2=3;x(2).f2=3;
3. result=arrayfun(@(x)isequal(x.f1,x.f2),x)
cast:转变数值数据类型  B=cast(A,newclass)
实例:
1. a=int16(2);
2. b=cast(a,'char');
3. cate=class(b)
cat:连接数组  C=cat(dim, A,B)--cat(2,A,B)与[A,B]相同,cat(1,A,B)与[A;B]相同
class:创建对象或者返回对象类型  obj=class(s,'class_name')
find:寻⾮零元素的索引和值
ind=find(X);X可以是逻辑式,返回⾮零元素的索引
ind=find(X, k, 'first') 返回数组X中所有⾮零元素的前k个元素。
ind=find(X, k, 'last')  返回数组X中所有⾮零元素的后k个元素。
[row,col,v]=find(X,...)返回列向量,⾏向量,v表⽰⾮零值
isequal :判断两个对象是否相等 ⽐较结构体是否相等时,结构体中⼦域的顺序不重要  NaNs在此函数中认识不相等---isequal(A,B,...)相同返回1,不同返回0
isequalwithequalnans:把NaN视为相等的情况下判断两个数组是否相等
2)字符串转化数值函数
base2dec :把指定进制的数字字符串转换为⼗进制数  d=base2dec('strn', base)
bin2dec :把⼆进制数字字符串转换为⼗进制数  bin2dec(binarystr)
hex2dec :把⼗六进制数字字符串转换为⼗进制数  d=hex2dec('hex_value')
hex2num :把⼗六进制数字字符串转换为双精度浮点数 n=hex2num(S)
str2double :把字符串转换为双精度浮点数
str2num :把字符串转换为数字
3)数值转化为字符串函数
dec2base:把⼗进制数转换为指定基下的数值字符串 str=dec2base(d, base)
dec2bin :把⼗进制数转换为⼆进制数值字符串
dec2hex :把⼗进制数转换为⼗六进制的数值字符串
int2str :把整数转换为字符串
mat2str :把矩阵转换为字符串
num2str :把数字转换为字符串
四、⾼级数据类型函数
1)结构体函数
cell2struct :把元胞数组转换成结构体数组 s=cell2struct(c, fields,dim) fields指结构体数组的域名,fields可以说字符数组或者字符串元胞数组。
size(c,dim)==length(fields)%如果fields是元胞数组 size
(c,dim)==size(fields,1)%如果fields是字符数组
实例:
1. c={'Tom','math',80;'Jane','math',70}
2. feilds={'name','subject','grade'};
3. s=cell2struct(c,fields,2)
struct:创建结构体数组 s=struct('field1', values1, 'field2',values2, ...)
struct2cell :把结构体数组转换为元胞数组
structfun :把函数应⽤于结构体中的每⼀个⼦域 A=structfun(fun, s)
2)元胞数组函数
cell :创建元胞数组    c=cell(n)c=cell(m,n)
celldisp :显⽰元胞数组的内容
cellfun :把函数应⽤于元胞数组中的每个元素  A=cellfun(fun, C, D,...)
cellplot :以图形形式显⽰元胞数组的结构
实例:
1. c{1,1}='2-by-2';c{1,2}='eigenvalues ofeye(2)';
2. c{2,1}=eye(2);c{2,2}=eig(eye(2));
3. cellplot(c)
cellstr :根据字符串数组创建字符串元胞数组
mat2cell :把矩阵分割为元胞数组  c=mat2cell(x,m, n)
num2cell :把数值数组转变为元胞数组  C=num2cell(A)
3)字符串函数
func2str :根据函数句柄构建函数名称字符串  s=func2str(fhandle)
char :把整数数组转化为字符串  S=char(X)
eval :执⾏包含MATLAB表达式的字符串  eval(expression)
findstr :在长字符串中寻匹配的短字符串,返回字符串索引
sprintf :把⼀定格式把数据写⼊字符串  [s, errmsg]=sprintf(format,A, ...)
strcat :⽔平连接字符串  t=strcat(s1,s2, s3, ...)
strcmp,strcmpi :⽐较字符串---strcmp('str1','str2')相同返回1,不同返回0
strings函数:连接字符串name=['abc' 'def' 'ghj']
strread :从字符串中读取指定格式数据
strrep :发现和替换⼦字符串  str=strrep(str1, str2, str3) ---将str1内的所有⼦字符串str2替换为str3 strtrim :删除字符串开头和结尾的空字符
blanks :创建空格字符串数组
数组转换成字符串
isletter :判断字符串中字符是否为英⽂字母
isspace :判断字符串中字符是否为空格
isstrprop :判断字符串中是否含有指定类别的字符tf=isspace('str')
dblank :删除字符串末尾空格

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。