|
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)?
|