相关疑难解决方法(0)

在MATLAB中组合两个结构有哪些有效的方法?

我想结合两个具有不同字段名称的结构.

例如,从以下开始:

A.field1 = 1;
A.field2 = 'a';

B.field3 = 2;
B.field4 = 'b';
Run Code Online (Sandbox Code Playgroud)

我想拥有:

C.field1 = 1;
C.field2 = 'a';
C.field3 = 2;
C.field4 = 'b';
Run Code Online (Sandbox Code Playgroud)

有没有比使用"fieldnames"和for循环更有效的方法?

编辑:我们假设在字段名称冲突的情况下,我们优先考虑A.

merge matlab field structure

24
推荐指数
3
解决办法
2万
查看次数

如何在Matlab中向结构数组添加新元素?

如何向结构数组添加新元素?我无法与空结构连接:

>> a=struct;
>> a.f1='hi'

a = 

    f1: 'hi'

>> a.f2='bye'

a = 

    f1: 'hi'
    f2: 'bye'

>> a=cat(1,a,struct)
Error using cat
Number of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of
fields.
Run Code Online (Sandbox Code Playgroud)

那么是否可以添加具有空字段的新元素?

更新

我发现如果我同时添加新字段,则可以添加新元素:

>> a=struct()

a = 

struct with no fields.

>> a.f1='hi';
>> a.f2='bye';
>> a(end+1).iamexist=true

a = 

1x2 struct array with fields:

    f1
    f2
    iamexist
Run Code Online (Sandbox Code Playgroud)

令人难以置信的是,没有直接的方法!可能有一些冒号等效的结构吗?

arrays matlab struct

5
推荐指数
2
解决办法
4万
查看次数

如何在Matlab中简单地将具有不同字段的两个结构连接起来?

有没有简单的方法可以组合以下两个结构,而无需使用for循环或CELLFUN?

struct1 = 

    a: {43x1 cell}

struct2 = 

    b: [43x1 double]
    c: {43x1 cell}
Run Code Online (Sandbox Code Playgroud)

我想要这样的组合结构:

struct3 = 

    a: {43x1 cell}
    b: [43x1 double]
    c: {43x1 cell}
Run Code Online (Sandbox Code Playgroud)

matlab struct

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

matlab ×3

struct ×2

arrays ×1

field ×1

merge ×1

structure ×1