Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: problem concatenating structs
Date: Tue, 25 Nov 2008 08:43:02 +0000 (UTC)
Organization: Lunds Universitet
Lines: 32
Message-ID: <gggdqm$a7a$1@fred.mathworks.com>
References: <ggfcna$cuq$1@fred.mathworks.com> <z_HWk.7$Mr7.5@newsfe01.iad>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1227602582 10474 172.30.248.38 (25 Nov 2008 08:43:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 25 Nov 2008 08:43:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 5707
Xref: news.mathworks.com comp.soft-sys.matlab:503043


Walter Roberson <roberson@hushmail.com> wrote 

> Lars Barring wrote: 
8<<<<SNIP
> You cannot concatenate structs unless they have exactly the same set of fields
> in exactly the same order. Unless Mathworks changes this in some future version,
> you will have to find some other way of doing things.

Well, hopefully they do, because the minimal example points at some less than 
obvious (to me) behaviour :-)


> If you want dissimilar structures, use cell arrays. Otherwise, write a small
> routine:
> 
> FN = setdiff(fieldnames(C),fieldnames(D));
> for FI = 1 : length(FN); C(1).(FN) = repmat(D.(FN),0,0); end
> 
> C(k) = catstruct(C(k), D);
> 
> The repmat of size 0 0 is to get the right class in place; if you know that you
> are going to be using catstruct immediately afterwards, you should probably be 
> able to use [] (the empty array) instead of the repmat() call.

Thanks, this is probably what I will be forced to do. But this code snippet have to
be at many places be in the calling code rather than in a function, or in catstruct itself.

Sigh. 

Lars