orderfields
结构体数组的顺序字段
语法
说明
示例
创建一个包含多个字段的结构体。
S1 = struct('b',1,'B',2,'a',3,'A',4)
S1 = struct with fields:
b: 1
B: 2
a: 3
A: 4
对字段排序。此语法基于 ASCII 顺序按字段名称对字段排序。
S = orderfields(S1)
S = struct with fields:
A: 4
B: 2
a: 3
b: 1
创建两个结构体,它们具有相同字段,只是字段顺序不同。字段名称相同,但字段值不同。
S1 = struct('b',1,'B',2,'a',3,'A',4)
S1 = struct with fields:
b: 1
B: 2
a: 3
A: 4
S2 = struct('a',0,'b',20,'B',10,'A',0)
S2 = struct with fields:
a: 0
b: 20
B: 10
A: 0
对 S1
中的字段进行排序以匹配 S2
中的字段顺序。
S = orderfields(S1,S2)
S = struct with fields:
a: 3
b: 1
B: 2
A: 4
创建一个结构体。
data.x = linspace(0,2*pi);
data.y = sin(data.x);
data.title = 'y = sin(x)'
data = struct with fields:
x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 … ] (1×100 double)
y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 … ] (1×100 double)
title: 'y = sin(x)'
通过以元胞数组形式列出字段名称来对字段排序。
C = {'title','x','y'}; data = orderfields(data,C)
data = struct with fields:
title: 'y = sin(x)'
x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 … ] (1×100 double)
y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 … ] (1×100 double)
创建一个结构体。
data.x = linspace(0,2*pi);
data.y = sin(data.x);
data.title = 'y = sin(x)'
data = struct with fields:
x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 … ] (1×100 double)
y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 … ] (1×100 double)
title: 'y = sin(x)'
通过以另一顺序列出字段的原始位置来对字段排序。例如,移动第三个字段,使其成为输出结构体的第一个字段。
P = [3 1 2]; data = orderfields(data,P)
data = struct with fields:
title: 'y = sin(x)'
x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 … ] (1×100 double)
y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 … ] (1×100 double)
创建一个结构体。
data1.x = linspace(0,2*pi);
data1.y = sin(data1.x);
data1.title = 'y = sin(x)';
使用 orderfields
函数对该结构体重新排序。将新字段顺序存储在置换向量 Pout
中。
[S,Pout] = orderfields(data1,{'title','x','y'})
S = struct with fields:
title: 'y = sin(x)'
x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 … ] (1×100 double)
y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 … ] (1×100 double)
Pout = 3×1
3
1
2
创建另一个具有相同字段的结构体。
data2.x = data1.x;
data2.y = cos(data2.x);
data2.title = 'y = cos(x)';
使用 Pout
对 data2
的字段重新排序。如果您有许多具有相同字段名称的结构体,则可以使用 Pout 以相同的方式对所有这些结构体的字段重新排序。
S2 = orderfields(data2,Pout)
S2 = struct with fields:
title: 'y = cos(x)'
x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 … ] (1×100 double)
y: [1 0.9980 0.9920 0.9819 0.9679 0.9501 0.9284 0.9029 0.8738 0.8413 0.8053 0.7660 0.7237 0.6785 0.6306 0.5801 0.5272 0.4723 0.4154 0.3569 0.2969 0.2358 0.1736 0.1108 0.0476 -0.0159 -0.0792 -0.1423 -0.2048 -0.2665 -0.3271 … ] (1×100 double)
输入参数
输入结构体,指定为结构体数组。
以结构体给出的字段顺序,指定为结构体数组。S2
与 S1
具有相同的字段,但以不同顺序指定。
以名称给出的字段顺序,指定为字符向量元胞数组或字符串数组。C
中的名称必须与 S1
的字段名称匹配。
以编号排列的字段顺序,指定为数值向量。编号必须为从 1
到 n
的整数,其中 n
是 S1
的字段数。
输出参量
重新排序后的结构体,以结构体数组形式返回。S
与 S1
具有相同的字段,但字段顺序可能不同。
输出字段顺序,以数值向量形式返回。Pout
的元素是从 1
到 n
的整数,其中 n
是 S1
的字段数。整数的置换表示字段顺序的变化。
提示
orderfields
函数仅对顶层字段排序。它不能递归。
扩展功能
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
版本历史记录
在 R2006a 之前推出
另请参阅
struct
| isfield
| fieldnames
| setfield
| getfield
| rmfield
| struct2cell
| cell2struct
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)