【问题标题】:How to simply concatenate two structures with different fields in Matlab? [duplicate]如何在 Matlab 中简单地连接具有不同字段的两个结构? [复制]
【发布时间】:2013-03-17 01:22:21
【问题描述】:

有没有什么简单的方法可以把下面两个结构组合起来,不用for循环或者CELLFUN?

struct1 = 

    a: {43x1 cell}

struct2 = 

    b: [43x1 double]
    c: {43x1 cell}

我想要这样的组合结构:

struct3 = 

    a: {43x1 cell}
    b: [43x1 double]
    c: {43x1 cell}

【问题讨论】:

标签: matlab struct


【解决方案1】:

我通过update struct via another struct in Matlab中的建议得到了我想要的,如下:

names = [fieldnames(struct1); fieldnames(struct2)];
struct3 = cell2struct([struct2cell(struct1); struct2cell(struct2)], names, 1);

【讨论】:

    【解决方案2】:

    考虑来自 Mathworks 的 this article and associated MATLAB Exchange script:

    主题:

    如何连接或合并两个结构?问题描述:

    我想将两个结构合并成一个新结构,其中包含 两个原始结构的所有字段。我怎样才能做到这一点 MATLAB?

    解决方案:

    MATLAB 中没有可用于连接的直接能力 结构。

    附件mergeStructs.m 显示了一些可以被 用于在 MATLAB 中合并结构。

    MATLAB Central 用户社区也有在线提交 你可以使用。一种这样的提交是:

    http://www.mathworks.com/matlabcentral/fileexchange/7842

    请注意,MathWorks 不保证或担保使用或内容 这些提交。任何问题、问题或投诉都应 指向特约作者。

    【讨论】:

    • 是的,我看过这篇文章,但它使用 for 循环进行连接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多