一、Cell
如果在一个类中用到多个类型的数据,我们如下定义
>> [12,'a',1:2:9,"hello"]
ans =
1×8 string 数组
"12" "a" "1" "3" "5" "7" "9" "hello"
>>
这样的话只要里面有一个char 类型的就有问题了
cell :
>> cellarray={23,'a',1:3:9,"hello"}
cellarray =
1×4 cell 数组
{[23]} {'a'} {[1 4 7]} {["hello"]}
>> cellarray(2)
ans =
1×1 cell 数组
{'a'}
>> cellarray(4)
ans =
1×1 cell 数组
{["hello"]}
>> cellmat(3,4)
未定义与 'double' 类型的输入参数相对应的函数 'cellmat'。
>> cell(3,4)
ans =
3×4 cell 数组
{0×0 double} {0×0 double} {0×0 double} {0×0 double}
{0×0 double} {0×0 double} {0×0 double} {0×0 double}
{0×0 double} {0×0 double} {0×0 double} {0×0 double}
>>
>> cellarray={[23],'a',"string",[1:2:17]}
cellarray =
1×4 cell 数组
{[23]} {'a'} {["string"]} {[1 3 5 7 9 11 13 15 17]}
>> cellarray(1)=[] 这个控制的是单元cell
cellarray =
1×3 cell 数组
{'a'} {["string"]} {[1 3 5 7 9 11 13 15 17]}
>> cellarray{1}='b' // 这种方法控制的则是单元格里面的内容
cellarray =
1×3 cell 数组
{'b'} {["string"]} {[1 3 5 7 9 11 13 15 17]}
>>
二、结构体
>> onepart = struct('part_No',123,'quantity',5,'price',29.5,'code','g')
onepart =
包含以下字段的 struct:
part_No: 123
quantity: 5
price: 29.5000
code: 'g'
>> class(onepart)
ans =
'struct'
>> onepart.part_No
ans =
123
>> onepart.quantity
ans =
5
>>
>> onepart.quantity=5
onepart =
包含以下字段的 struct:
part_No: 123
quantity: 5
price: 29.5000
code: 'g'
>>
>> rmfield(newpackage,'code')
ans =
包含以下字段的 struct:
item_No: 123
cost: 19.0900
price: 29.5000
>>
ex:
% structure(s)
function myfunc_profit=myfunc6(packstruct)
myfunc_profit=packstruct.price-packstruct.cost;
end
>> myfunc_profit=myfunc6(newpackage)
myfunc_profit =
10.4100
>>
逗号分割 和数组的转化
>> temp=[89,'1'] 吧数字变成char 类型的
temp =
'Y1'
>>
>> temp2=[89,'1',"89"] // 将类型变成【】中的最复杂的类型
temp2 =
1×3 string 数组
"89" "1" "89"
>> temp3=['1',"898",99,rand(1,3)]
temp3 =
1×6 string 数组
"1" "898" "99" "0.81472" "0.90579" "0.12699"
>>
Sort 排序:
>> vec=[87 89 6 90 1 91]
vec =
87 89 6 90 1 91
>> sort(vec)
ans =
1 6 87 89 90 91
>> sort(vec,'descend')
ans =
91 90 89 87 6 1
>> sort(vec,'asscend')
错误使用 sort
排序方向必须为 'ascend' 或 'descend'。
>> sort(vec,'ascend')
ans =
1 6 87 89 90 91
>>
>> cylses(1)=struct('code','x','dimensions',struct('rad',3,'height',12,'width',12),'widthes',7)
cylses =
包含以下字段的 struct:
code: 'x'
dimensions: [1×1 struct]
widthes: 7
>> cylses(2)=struct('code','b','dimensions',struct('rad',3,'height',22,'width',12),'widthes',6)
cylses =
包含以下字段的 1×2 struct 数组:
code
dimensions
widthes
>> cylses(3)=struct('code','n','dimensions',struct('rad',3,'height',220,'width',92),'widthes',8)
cylses =
包含以下字段的 1×3 struct 数组:
code
dimensions
widthes
>> cylses
cylses =
包含以下字段的 1×3 struct 数组:
code
dimensions
widthes
>> cylses.widthes
ans =
7
ans =
6
ans =
8
>> cyls(1)
ans =
包含以下字段的 struct:
code: 'x'
dimensions: [1×1 struct]
>> cylses(1)
ans =
包含以下字段的 struct:
code: 'x'
dimensions: [1×1 struct]
widthes: 7
>>
sort 并且将原来的位置也打印出来
>> [w_sorted,i_sorted]=sort([cylses.widthes])
w_sorted =
6 7 8
i_sorted =
2 1 3
table
names={'harry','Sally','jose'};
weights=[185;178;190];
heights=[74;67.5;75];
patients=table(weights,heights,'RowNames',names);
>> patients
patients =
3×2 table
weights heights
_______ _______
harry 185 74
Sally 178 67.5
jose 190 75
>>
排序 按照体重来
>> [~,isweightssorted]=sort(patients(:,'weights'))
return
错误使用 tabular/sort
输出参数太多。
>> [~,isweightssorted]=sort(patients{:,'weights'})
isweightssorted =
2
1
3
>> patients(isweightssorted,:)
ans =
3×2 table
weights heights
_______ _______
Sally 178 67.5
harry 185 74
jose 190 75
>>
>> sortrows(patients,'heights')
ans =
3×2 table
weights heights
_______ _______
Sally 178 67.5
harry 185 74
jose 190 75
>>
>> sortrows(patients,'weights')
ans =
3×2 table
weights heights
_______ _______
Sally 178 67.5
harry 185 74
jose 190 75
// 是否已经排好序
>> issortedrows(patients,'heights')
ans =
logical
0
>> issortedrows(patients,'weights')
ans =
logical
0
>>