如何select数组结构中的一些数据
How to select some data in array structure
我在 MATLAB 环境中工作,我有一些像这样的结构:
>> HTC_01
HTC_01 =
Name: 'HTC_One_M8-2015-02-11-15-40-30'
Date: '2015-02-11'
Time: [395768x1 double]
Ax: [395768x1 double]
Ay: [395768x1 double]
Az: [395768x1 double]
Lat: [395768x1 double]
Lon: [395768x1 double]
Quo: [395768x1 double]
Vel: [395768x1 double]
现在,我想要 select 来自所有数组的一些数据,例如 (4646:279745),并将输出放入一个与该数组同名的新数组中。
我想获得:
>> HTC_02 = my_resize(HTC_01, 4646, 279745)
HTC_02 =
Name: 'HTC_One_M8-2015-02-11-15-40-30'
Date: '2015-02-11'
Time: [275100x1 double]
Ax: [275100x1 double]
Ay: [275100x1 double]
Az: [275100x1 double]
Lat: [275100x1 double]
Lon: [275100x1 double]
Quo: [275100x1 double]
Vel: [275100x1 double]
问题是:我必须一个数组一个数组地做,还是有更短的方法?
在我看来,仅调整数组的大小在 MATLAB 中非常简单,因此必须存在一种无需创建函数即可完成此操作的简便方法。
这将是一种方法 -
index_range = 4646:279745 %// index range
flds = {'Ax','Ay','Az','Lat','Lon','Quo','Vel'} %// fields to be selected
fnames = fieldnames(HTC_01) %// all fieldnames
%// Logical array with length as number of fields
%// and ones where the fields to be selected appear
idx = ismember(fnames,flds)
C = struct2cell(HTC_01) %// Get all of the data into a cell array
out1 = reshape([C{idx}],[],sum(idx)).' %//'#select fields
out2 = out1(:,index_range) %// select data from range
cell_out = mat2cell(out2,ones(1,size(out2,1)),size(out2,2))
%// Store truncated data into numeric fields and then save back as struct
C(idx) = cell_out
out = cell2struct(C,fnames,1)
仅适用于结构中的数组字段:
fields = fieldnames(HTC);
i = 1;
for x = 1:length(fields)
field = fields{x};
data = getfield(HTC,field);
if isa(data,'double')
output(:,i) = data(4646:279745);
i = i + 1;
end
end
所需的每个数据数组范围都保存为输出数组中的一列。
在您更新后更新:
function output = my_resize(input,r1,r2)
fields = fieldnames(input);
output = input;
for x = 1:length(fields)
field = fields{x};
data = getfield(input,field);
if isa(data,'double')
output = setfield(output,field,data(r1:r2,1));
end
end
我在 MATLAB 环境中工作,我有一些像这样的结构:
>> HTC_01
HTC_01 =
Name: 'HTC_One_M8-2015-02-11-15-40-30'
Date: '2015-02-11'
Time: [395768x1 double]
Ax: [395768x1 double]
Ay: [395768x1 double]
Az: [395768x1 double]
Lat: [395768x1 double]
Lon: [395768x1 double]
Quo: [395768x1 double]
Vel: [395768x1 double]
现在,我想要 select 来自所有数组的一些数据,例如 (4646:279745),并将输出放入一个与该数组同名的新数组中。
我想获得:
>> HTC_02 = my_resize(HTC_01, 4646, 279745)
HTC_02 =
Name: 'HTC_One_M8-2015-02-11-15-40-30'
Date: '2015-02-11'
Time: [275100x1 double]
Ax: [275100x1 double]
Ay: [275100x1 double]
Az: [275100x1 double]
Lat: [275100x1 double]
Lon: [275100x1 double]
Quo: [275100x1 double]
Vel: [275100x1 double]
问题是:我必须一个数组一个数组地做,还是有更短的方法?
在我看来,仅调整数组的大小在 MATLAB 中非常简单,因此必须存在一种无需创建函数即可完成此操作的简便方法。
这将是一种方法 -
index_range = 4646:279745 %// index range
flds = {'Ax','Ay','Az','Lat','Lon','Quo','Vel'} %// fields to be selected
fnames = fieldnames(HTC_01) %// all fieldnames
%// Logical array with length as number of fields
%// and ones where the fields to be selected appear
idx = ismember(fnames,flds)
C = struct2cell(HTC_01) %// Get all of the data into a cell array
out1 = reshape([C{idx}],[],sum(idx)).' %//'#select fields
out2 = out1(:,index_range) %// select data from range
cell_out = mat2cell(out2,ones(1,size(out2,1)),size(out2,2))
%// Store truncated data into numeric fields and then save back as struct
C(idx) = cell_out
out = cell2struct(C,fnames,1)
仅适用于结构中的数组字段:
fields = fieldnames(HTC);
i = 1;
for x = 1:length(fields)
field = fields{x};
data = getfield(HTC,field);
if isa(data,'double')
output(:,i) = data(4646:279745);
i = i + 1;
end
end
所需的每个数据数组范围都保存为输出数组中的一列。
在您更新后更新:
function output = my_resize(input,r1,r2)
fields = fieldnames(input);
output = input;
for x = 1:length(fields)
field = fields{x};
data = getfield(input,field);
if isa(data,'double')
output = setfield(output,field,data(r1:r2,1));
end
end