CATSTRUCT

버전 1.3.0.0 (4.95 KB) 작성자: Jos (10584)
Concatenate/merge structures (v4.1, feb 2015).
다운로드 수: 30.7K
업데이트 날짜: 2015/2/4

라이선스 보기

편집자 메모: This file was selected as MATLAB Central Pick of the Week

CATSTRUCT Concatenate or merge structures with different fieldnames
X = CATSTRUCT(S1,S2,S3,...) merges the structures S1, S2, S3 ... into one new structure X. X contains all fields present in the various
structures. An example:

A.name = 'Me' ;
B.income = 99999 ;
X = catstruct(A,B)
% -> X.name = 'Me' ;
% X.income = 99999 ;

If a fieldname is not unique among structures (i.e., a fieldname is present in more than one structure), only the value from the last structure with this field is used. In this case, the fields are alphabetically sorted. A warning is issued as well. An axample:

S1.name = 'Me' ;
S2.age = 20 ; S3.age = 30 ; S4.age = 40 ;
S5.honest = false ;
Y = catstruct(S1,S2,S3,S4,S5) % use value from S4

The inputs can be array of structures. All structures should have the same size. An example:

C(1).bb = 1 ; C(2).bb = 2 ;
D(1).aa = 3 ; D(2).aa = 4 ;
CD = catstruct(C,D) % CD is a 1x2 structure array with fields bb and aa

The last input can be the string 'sorted'. In this case,
CATSTRUCT(S1,S2, ..., 'sorted') will sort the fieldnames alphabetically. To sort the fieldnames of a structure A, you could use CATSTRUCT(A,'sorted') but I recommend ORDERFIELDS for doing that.

When there is nothing to concatenate, the result will be an empty struct (0x0 struct array with no fields).

NOTE
To concatenate similar arrays of structs, you can use simple concatenation:
A = dir('*.mat') ; B = dir('*.m') ; C = [A ; B] ;
Latest version: 4.0 (dec 2013)

인용 양식

Jos (10584) (2025). CATSTRUCT (https://www.mathworks.com/matlabcentral/fileexchange/7842-catstruct), MATLAB Central File Exchange. 검색 날짜: .

MATLAB 릴리스 호환 정보
개발 환경: R2014a
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Structures에 대해 자세히 알아보기
도움

도움 준 파일: Lynx MATLAB Toolbox, Structable

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
버전 게시됨 릴리스 정보
1.3.0.0

changed number of input arguments check to accommodate future releases. Probably this function will no longer work anymore in older releases :-(

1.2.0.0

Due to changes implemented by The Mathworks in their set functions, I had to explicitly tell unique to return the last occurrence of a value in a set ...

1.1.0.0

now deals correctly with arrays of structures (thanks to Tor Inge Birkenes for pointing out this problem)

1.0.0.0

fixed bug for empty structs