【问题标题】:Iterating through Struct MATLAB遍历 Struct MATLAB
【发布时间】:2017-04-30 13:32:21
【问题描述】:

以下是我目前所拥有的。我需要遍历rxnsBothKaletaS 结构的每个字段,其中每个字段代表一个nx4 单元格。我需要从该单元格的第一列中提取信息作为用逗号分隔的单个字符串,然后将其分配给fname 中的字段索引。

(给出上下文:fname 是基因的名称,每个基因中都是依赖于该基因的反应。我需要提取给定基因的所有反应并将它们全部作为字符串用逗号分隔)

fname = fieldnames(rxnsBothKaletaS)
for  i = 1:numel(fname)
    gene = rxnsBothKaletaS.(fname{i})

    for j = 1:size(gene,1)
        rxns = rxns + char(string(gene(j,1)));
    end 

    fname(i,2) = rxns; 
end

【问题讨论】:

    标签: matlab struct matlab-struct


    【解决方案1】:

    您可以使用structfun 来遍历结构的字段。然后在我们将应用于每个字段的匿名函数中,我们可以使用strjoinx 第一列中的所有字符串与逗号连接起来。

    fnames = structfun(@(x)strjoin(x(:,1), ','), rxnsBothKaletaS, 'UniformOutput', 0);
    

    【讨论】:

    • 我收到一个错误:使用 strjoin 时出错(第 55 行)第一个输入必须是字符数组的元胞数组。 model_comparison 中的错误>@(x)strjoin(x(:,1),',') model_comparison 中的错误(第 17 行) fnames = structfun(@(x)strjoin(x(:,1), ','), rxnsBothKaletaS, 'UniformOutput', 0);
    • @SeanSadykoff 如果不是字符串,您的N x 4 元胞数组里面是什么?
    猜你喜欢
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    相关资源
    最近更新 更多