在 Matlab 中将单元格转换为结构体
Convert Cell to Struct in Matlab
我正在尝试访问具有 4 个结构的元胞数组的内容。
celldisp(tracks_array);
给出输出:
tracks_array{1} =
kalmanFilter: [1×1 vision.KalmanFilter]
id: 0
totalVisibleCount: 1
bbox: [390 171 70 39]
consecutiveInvisibleCount: 0
age: 1
tracks_array{2} =
kalmanFilter: [1×1 vision.KalmanFilter]
id: 1
totalVisibleCount: 1
bbox: [459 175 40 24]
consecutiveInvisibleCount: 0
age: 1
tracks_array{3} =
kalmanFilter: [1×1 vision.KalmanFilter]
id: 2
totalVisibleCount: 1
bbox: [220 156 159 91]
consecutiveInvisibleCount: 0
age: 1
tracks_array{4} =
kalmanFilter: [1×1 vision.KalmanFilter]
id: 3
totalVisibleCount: 1
bbox: [510 159 68 49]
consecutiveInvisibleCount: 0
age: 1
然后我使用for循环遍历元素..
for elmen = tracks_array
structtt=cell2struct(elmen(1),{'id','bbox','kalmanFilter','age','totalVisibleCount','consecutiveInvisibleCount'},2);
这给出了
的错误
Error using cell2struct
Number of field names must match number of fields in new structure.
然后我在 for 循环中使用了它
disp(elmen)
celldisp(elmen)
给予,
[1×1 struct]
elmen{1} =
kalmanFilter: [1×1 vision.KalmanFilter]
totalVisibleCount: 1
bbox: [390 171 70 39]
consecutiveInvisibleCount: 0
id: 0
age: 1
我想通过字段名称访问元素。我该怎么做?
现在,如果我尝试使用 getfield,它会出现此错误:
Struct contents reference from a non-struct array object.
当使用 for
遍历元胞数组时,Matlab 中有一个奇怪的地方。您可能希望它在每次迭代时为您提供实际元素,但它却为您提供了一个仅包含一个值的元胞数组,即该元素。但是使用 elmen{1}
提取实际元素很容易。所以在你的代码示例中:
for elmen = tracks_array
% The actual element is the first and only entry in the cell array
structtt = elmen{1};
% Display the structure
disp(structtt);
% Display a field within the structure
disp(structtt.totalVisibleCount);
% The same as above, but using getfield()
disp(getfield(structtt, 'totalVisibleCount'));
end
您还可以将上面的代码编写为元胞数组的 for 循环,使用 {}
语法提取每个元素
for index = 1 : length(tracks_array)
structtt = tracks_array{index};
% Display the structure
disp(structtt);
% Display a field within the structure
disp(structtt.totalVisibleCount);
% The same as above, but using getfield()
disp(getfield(structtt, 'totalVisibleCount'));
end
我正在尝试访问具有 4 个结构的元胞数组的内容。
celldisp(tracks_array);
给出输出:
tracks_array{1} =
kalmanFilter: [1×1 vision.KalmanFilter]
id: 0
totalVisibleCount: 1
bbox: [390 171 70 39]
consecutiveInvisibleCount: 0
age: 1
tracks_array{2} =
kalmanFilter: [1×1 vision.KalmanFilter]
id: 1
totalVisibleCount: 1
bbox: [459 175 40 24]
consecutiveInvisibleCount: 0
age: 1
tracks_array{3} =
kalmanFilter: [1×1 vision.KalmanFilter]
id: 2
totalVisibleCount: 1
bbox: [220 156 159 91]
consecutiveInvisibleCount: 0
age: 1
tracks_array{4} =
kalmanFilter: [1×1 vision.KalmanFilter]
id: 3
totalVisibleCount: 1
bbox: [510 159 68 49]
consecutiveInvisibleCount: 0
age: 1
然后我使用for循环遍历元素..
for elmen = tracks_array
structtt=cell2struct(elmen(1),{'id','bbox','kalmanFilter','age','totalVisibleCount','consecutiveInvisibleCount'},2);
这给出了
的错误Error using cell2struct
Number of field names must match number of fields in new structure.
然后我在 for 循环中使用了它
disp(elmen)
celldisp(elmen)
给予,
[1×1 struct]
elmen{1} =
kalmanFilter: [1×1 vision.KalmanFilter]
totalVisibleCount: 1
bbox: [390 171 70 39]
consecutiveInvisibleCount: 0
id: 0
age: 1
我想通过字段名称访问元素。我该怎么做?
现在,如果我尝试使用 getfield,它会出现此错误:
Struct contents reference from a non-struct array object.
当使用 for
遍历元胞数组时,Matlab 中有一个奇怪的地方。您可能希望它在每次迭代时为您提供实际元素,但它却为您提供了一个仅包含一个值的元胞数组,即该元素。但是使用 elmen{1}
提取实际元素很容易。所以在你的代码示例中:
for elmen = tracks_array
% The actual element is the first and only entry in the cell array
structtt = elmen{1};
% Display the structure
disp(structtt);
% Display a field within the structure
disp(structtt.totalVisibleCount);
% The same as above, but using getfield()
disp(getfield(structtt, 'totalVisibleCount'));
end
您还可以将上面的代码编写为元胞数组的 for 循环,使用 {}
语法提取每个元素
for index = 1 : length(tracks_array)
structtt = tracks_array{index};
% Display the structure
disp(structtt);
% Display a field within the structure
disp(structtt.totalVisibleCount);
% The same as above, but using getfield()
disp(getfield(structtt, 'totalVisibleCount'));
end