Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news2.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post02.iad.highwinds-media.com!newsfe01.iad.POSTED!7564ea0f!not-for-mail
From: Walter Roberson <roberson@hushmail.com>
Organization: Canada Eat The Cookie Foundation
User-Agent: Thunderbird 2.0.0.18 (Windows/20081105)
MIME-Version: 1.0
Newsgroups: comp.soft-sys.matlab
Subject: Re: problem concatenating structs
References: <ggfcna$cuq$1@fred.mathworks.com>
In-Reply-To: <ggfcna$cuq$1@fred.mathworks.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 37
Message-ID: <z_HWk.7$Mr7.5@newsfe01.iad>
NNTP-Posting-Host: 24.79.146.116
X-Complaints-To: internet.abuse@sjrb.ca
X-Trace: newsfe01.iad 1227574303 24.79.146.116 (Tue, 25 Nov 2008 00:51:43 UTC)
NNTP-Posting-Date: Tue, 25 Nov 2008 00:51:43 UTC
Date: Mon, 24 Nov 2008 18:52:01 -0600
Xref: news.mathworks.com comp.soft-sys.matlab:502987


Lars Barring wrote:

> I have some problems concatenating structs.
> Here is a minimal example

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.

> In reality I am using Jos' excellent FEX contribution CATSTRUCT:
 
> D=C(k);
> D.newfield=xyz;
> %etc
> C(k)=catstruct(C(k),D);   % fails
 
> CATSTRUCT works as expected but because there is an index to C the line fails.
 
> Any ideas how to solve this?

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.

-- 
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?