Code covered by the BSD License  

Highlights from
CATSTRUCT

4.64286

4.6 | 14 ratings Rate this file 127 Downloads (last 30 days) File Size: 2.02 KB File ID: #7842

CATSTRUCT

by Jos (10584)

 

13 Jun 2005 (Updated 03 Oct 2008)

Concatenate structures (v2.2, oct 2008).

| Watch this File

File Information
Description

X = CATSTRUCT(S1,S2,S3,...) concates the structures S1, S2, ... into one structure X.

CATSTRUCT(S1,S2,'sorted') will sort the fieldnames alphabetically.

If a fieldname occurs more than once in the argument list, only the last occurence is used, and the fields are alphabetically sorted.

To sort the fieldnames of a structure A use: A = CATSTRUCT(A,'sorted') ;

Latest version: 2.2 (oct 2008)

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (17)
28 Oct 2006 Paolo de Leva

This function is a must for people using structures. I wonder why MATLAB doesn't allow (as far as I know) structure concatenation using the [S1, S2] syntax, when the two structures have different fields. The builtin function STRUCT can't add fields to an existing structure...

16 Apr 2007 Nadaraja Pillai

Hi ,
       It is possible to concates the structure simply using collon... For example...

X=[S1:S2:S3....]

It will do it...

04 May 2007 Jingzhao Ou

A very useful script!

21 Sep 2007 Jos (the author)

new version 2.0 (sep 2007). A bug was removed which ocured when fields contained cell arrays.

21 Feb 2008 Hoi Wong

Cool program. I can't imagine why MATLAB wouldn't have something like this built-in, but you filled the gap!

28 May 2008 Maziar Hashemi-Nezhad

Perfect basic program
Thanks

04 Aug 2008 Piotr .

Very useful function!

29 Sep 2008 Benny .

Good job. It would be very usefull if it also worked on general structures, not only those containing numerical matrices only, e.g.

a = dir('*.txt');
b = dir('*.mat');
c = catstruct(a,b);

29 Sep 2008 Jos (the author)

To Benny. Catstruct does work on structures with non-numeric fields (see the example). You're after simple concatenation, for which you do not need catstruct at all ...

a = dir('*.mat')
b = dir('*.txt')
c = [a ; b] ;

11 Dec 2008 Daniel  
22 Jan 2009 Jeremy

Nice work, just one comment:
I just noticed that when merging two structures, the fact that any dissimilar fields in the FIRST level are merged, but dissimilarities in subsequent levels are lost: e.g.

s1.a = 1;
s1.b = 2;

s2.a = 101;

s3 = catstruct(s1,s2) gives s3.a = 101, s3.b = 2.
But

s1.A.a = 1;
s1.A.b = 2;

s2.A.a = 101;

gives s3.A = a : 101.
i.e. we lose field b. I kind of understand the logic, i.e. that it is now field A that is being overwritten by a new field A, so that a depth=1 merge has occurred, but for anyone looking to do a true merge-style operation, this means needing to loop though each level... any plans to change this?

08 Apr 2009 Gabriel Akira Schreiber

great work, may the sun always shine on you.

17 Jul 2009 Scott Otterson

This works well. I agree with Jeremy that recursive concatenation would be an advantage. It would also be useful to concatenate arrays and cells at the leaf nodes in the case when the fieldnames match (I've had to do this many times).

But it works for what I'm doing at the moment. Thanks!

10 Feb 2011 K E

Should be standard in Matlab

14 Feb 2011 grega  
17 Dec 2011 Wouter  
17 Dec 2011 Wouter

I also needed to concatenate arrays and cells at the leaf nodes in the case when the fieldnames match. This can be done by slightly adjusting the code as:
if numel(UFN) ~= numel(FN),
    warning('catstruct:DuplicatesFound','Duplicate fieldnames found. Fields are merged and sorted.') ;
    Nd = numel(unique(j));
    for i=1:Nd
        idx = find(j==unique(j(i)));
        [nr,nc] = cellfun(@size, VAL(idx));
        % comment if fields are not required to be equal in size:
        if diff(nr) || diff(nc);error(['Duplicate field ' FN{idx(1)} ' does not have the same size for all structures.']);end
        [p,rcm] = min([nc(1) nr(1) 2]);
        % concatenate rows as rows, colums as colomns and matrices along 2nd dimension.
        VAL{idx(end)} = cat(min(rcm,2),VAL{idx});
    end
    sorted = 1;
end

Please login to add a comment or rating.
Updates
21 Jun 2006

added semi-colons

24 Sep 2007

removed bug when delaing with fields containing cell arrays

24 Sep 2007

add keywords

16 Sep 2008

added identifiers

03 Oct 2008

fixed bug for empty structs

Tag Activity for this File
Tag Applied By Date/Time
structures Jos (10584) 22 Oct 2008 07:50:25
cell arrays Jos (10584) 22 Oct 2008 07:50:25
structure Jos (10584) 22 Oct 2008 07:50:25
struct Jos (10584) 22 Oct 2008 07:50:25
structs Jos (10584) 22 Oct 2008 07:50:25
concatenate Jos (10584) 22 Oct 2008 07:50:25
merge Jos (10584) 22 Oct 2008 07:50:25
combine Jos (10584) 22 Oct 2008 07:50:25
cell arrays Yossi Michaeli 14 Feb 2010 13:06:46
combine Adam 23 Aug 2010 18:09:13
concatenate Charles 09 Nov 2010 12:10:57
merging Jos (10584) 16 Mar 2011 03:11:24
combine Zhanhong 28 Mar 2011 12:33:40
concatenate Ania 24 Oct 2011 09:39:50
concatenate Graziano 02 Nov 2011 13:23:08

Contact us at files@mathworks.com