help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fwd: Re: struct concatenation


From: Julien Bect
Subject: Fwd: Re: struct concatenation
Date: Thu, 7 Jul 2016 09:32:06 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.1.0

Forwarding to help list.


-------- Message transféré --------
Sujet : Re: struct concatenation
Date : Thu, 7 Jul 2016 09:31:23 +0200
De : Julien Bect <address@hidden>
Organisation : CentraleSupelec
Pour : address@hidden


Le 07/07/2016 à 09:23, c. a écrit :
> Hi,
>
> Say you have two struct variables
>
>>> s1
> s1 =
>
>    scalar structure containing the fields:
>
>      a: 1x1 scalar
>      b: 1x1 scalar
>
>>> s2
> s2 =
>
>    scalar structure containing the fields:
>
>      c: 1x1 scalar
>      d: 1x1 scalar
>
> and I want to create a new struct s that contains all fields of both.
> what would be a clean way of doing this?
>
> All I came up with for the moment was cycling over s1 and s2 and assigning
> each field to s, is there a better way?

here one possible way:

C = [fieldnames(s1) struct2cell(s1); fieldnames(s2) struct2cell(s2)];
s = struct (C'{:});

I don't know it it exists somewhere, but perhaps could this be turned 
into a function name fieldcat (same as horzcat, vertcat) ?

@++
Julien


reply via email to

[Prev in Thread] Current Thread [Next in Thread]