根据数据类型从单元格数组中提取元素

时间:2017-08-22 17:10:21

标签: arrays matlab

我使用函数qxf2mat将qxf数据加载到MATLAB中。这将创建以下1x1结构:

struct with fields:

                 qxfName: 'BSR1786214'
        numberOfChannels: 14
          numberOfCycles: 24477
          parameterCodes: {1×14 cell}
          parameterTitle: {1×14 cell}
     parameterShortTitle: {1×14 cell}
     parameterDefinition: {1×14 cell}
    minimumObservedValue: [1×14 double]
    maximumObservedValue: [1×14 double]
             absentValue: [1×14 double]
                datetime: [24477×20 char]
                BTVOLTCM: [1×24477 single]
          BTVOLTCM_flags: {1×24477 cell}
                HEADCM01: [1×24477 single]
          HEADCM01_flags: {1×24477 cell}
                ISCMBMA1: [1×24477 single]

我现在要做的是提取所有[1x24477单个]元素并将它们放入自己的矩阵中。因此矩阵将有24477行,在本例中为3列。

我已使用struct2cell将结构转换为单元格数组,并计划在此之后使用cell2mat,但由于所有不同的数据类型,我无法使用

1 个答案:

答案 0 :(得分:1)

这是一种做法:

% Get the names of the fields
fnames=fieldnames(mystruct);

% Get the fields that are both not a cell, and the correct size
thisones=~structfun(@iscell,mystruct)&structfun(@(x)(size(x,2)==mystruct.numberofCycles),mystruct);

% to make sure they are always 3 (else the struct is different of what you showed)
assert(sum(thisones)==3);

%Get the index of the fields
indices=find(thisones);

% make a matrix by appending the columns
result=zeros(3,mystruct.numberofCycles);
for ii=1:3
   result(ii,:)=mystruct.(fnames{indices(ii)});
end

请注意,如果您有2个大小为1xN但结构不同但不是单元格的结构,则此方法无效。您需要使thisones条件更复杂。