Arrays 基于数据类型的单元数组元素提取

Arrays 基于数据类型的单元数组元素提取,arrays,matlab,Arrays,Matlab,我正在使用函数qxf2mat将qxf数据加载到MATLAB中。这将创建以下1x1结构: struct with fields: qxfName: 'BSR1786214' numberOfChannels: 14 numberOfCycles: 24477 parameterCodes: {1×14 cell} parameterTitle: {1×14 cell} para

我正在使用函数
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 single]元素,并将它们放入它们自己的矩阵中。因此,矩阵将有24477行,在本例中,有3列


我已经使用了
struct2cell
将结构转换为单元格数组,并计划在此之后使用
cell2mat
,但由于数据类型不同,我不能这样做。

这里是一种方法:

% 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

请注意,如果您有两个大小为1xN的结构属于不同类型,但不是单元,则这将不起作用。您需要将
thisones
条件变得更复杂。

仅凭名称访问它们不是更容易吗?或者名称会改变吗?名称会随着数据集的不同而变化,因此理想情况下,我只想通过索引访问它们。您事先知道大小,还是从
numberofycles
中获取?仅从numberofycleskay中获取,因此对于我的完整示例,我有14列(而不是3列),这由numberOfChannels参数表示。我尝试在代码中用14替换3,但最后一步失败:“索引超过矩阵维度”。我是否也应该更改大小(x,2)部分?@BradReed如果失败,那一定是因为您没有在
result=zeros…
中替换它,或者因为它没有找到14个字段来破解代码,只运行其中一个索引,比如
ii=14
fnames{index(14)}
工作正常,但在尝试运行
mystruct(fnames{index(14)})
@BradReed什么是
size(index)
?13? 然后它只找到了13个字段。但是没有真正的案例我无法调试不,是14。是的,我知道,对不起,没有数据我很感激。