【问题标题】:Modify struct array and return struct array修改结构体数组并返回结构体数组
【发布时间】:2015-08-05 20:26:41
【问题描述】:

我有一个结构体数组:一个 1x10 结构体数组,其中包含以下字段:N、t、q、r、T,每个字段都是double 类型的向量。

10 个数组条目分别代表实验中测试条件的结果。我希望能够制作一个函数,它采用两个索引index1index2,并修改组成的 N、t、q、r 向量(T 是单个数字),使它们变成长度 index1 :index2。像

function sa = modifier(struct_array, index1, index2)
    sa = structfun(@(x) x(index1:index2), struct_array, 'UniformOutput', false)
    stuff
end

现在,stuff 所在的位置,我尝试使用 structfuncellfun,请参阅 here,除了它们分别返回一个结构体和一个元胞数组,而我需要返回一个 结构数组

这样做的目的是为了能够得到某些部分的实验结果,例如也许每个单元格内每个向量中的前五个条目对应于实验的初始周期。

请让我知道这是否可行,以及我将如何去做!

【问题讨论】:

    标签: arrays matlab struct


    【解决方案1】:

    你可以试试这个:

    从这个问题的answer,我想出了如何循环遍历结构字段。我修改了代码来解决您的问题,方法是从通过 for 循环的每个字段中提取子样本,然后将该数据的所需子集复制到具有相同名称字段的新结构数组中。

    % Define indexes for extraction
    fieldsToTrim = {'a' 'b'};
    idx = 2:3; % Create index vector for extracting selected data range
    
    % Define test struct to be read
    teststruct.a = [1 2 3];
    teststruct.b = [4 5 6];
    teststruct.c = [7 8 9];
    
    % Get names of struct fields
    fields = fieldnames(teststruct);
    
    % Loop through each field and extract the subset
    for i = 1:numel(fields)
        if max(strcmp(fields{i},fieldsToTrim)) > 0
            % If current matches one of the fields selected for extraction
            % extract subset
            teststructResults.(fields{i}) = teststruct.(fields{i})(idx);
        else
            % Else, copy all contents on field to resulting struct
            teststructResults.(fields{i}) = teststruct.(fields{i});
        end
    end
    

    最后要将this变成一个函数,可以将上面的代码修改成这样:

    function teststructResults =  extractSubsetFromStruct(teststruct,fieldsToTrim,idx1, idx2)
        % idx1 and idx2 are the start and end indicies of the desired range
    
        % fieldsToTrim is a string array of the field names you want
        % included in the trimming, all other fields will be fully copied
    
        % teststruct is your input structure which you are extracting the
        % subset from
    
        % teststructResults is the output containing identically named
        % struct fields to the input, but only containing data from the selected range
    
        idx = idx1:idx2; % Create index vector for extracting selected data range
    
        % Get names of struct fields
        fields = fieldnames(teststruct);
    
        % Loop through each field and extract the subset
        for i = 1:numel(fields) 
            if max(strcmp(fields{i},fieldsToTrim)) > 0
                % If current matches one of the fields selected for extraction
                % extract subset
                temp = teststruct.(fields{i});
                teststructResults.(fields{i}) = temp(idx);
            else
                % Else, copy all contents on field to resulting struct
                teststructResults.(fields{i}) = teststruct.(fields{i});
            end
        end
    end
    

    我成功运行了这样的函数:

    teststruct = 
    
        a: [1 2 3]
        b: [4 5 6]
        c: [7 8 9]
    
    >> extractSubsetFromStruct(teststruct,{'a' 'b'},2,3)
    
    ans = 
    
        a: [2 3]
        b: [5 6]
        c: [7 8 9]
    

    【讨论】:

    • 我收到此错误:Field reference for multiple structure elements that is followed by more reference blocks is an error.
    • hmm.. 给我一分钟,我会重新测试代码并重新发布!
    • K,更新了,我对函数做了一些改动。
    • 我遇到的问题是teststructResults.(fields{i}) = teststruct.(fields{i})(idx) 不会评估。一旦选择了第 i 个字段名,teststruct.(fields{i}) 就可以了,因为它会返回结构中该名称的所有字段,但不允许返回 每个字段的 2:4 元素.
    • 您最近的更新介绍了一个中间 temp 对我来说已经成功了。非常感谢你,@James。
    猜你喜欢
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    相关资源
    最近更新 更多