5.0

5.0 | 3 ratings Rate this file 141 downloads (last 30 days) File Size: 3.26 KB File ID: #21209

SORTFIELDS

by Ian Howat

 

24 Aug 2008 (Updated 25 Aug 2008)

No BSD License  

SORTFIELDS sort values in all fields within a structure.

Download Now | Watch this File

File Information
Description

%SORTFIELDS sort values in all fields within a structure.
% SORTFIELDS(A) sorts the rows of each field of structure A in asending
% order and returns a structure of the same dimension and field names.
% SORTFIELDS(A,'SORTBYFIELD','FIELDNAME',...) sorts each field by the
% specified field. All fields must be the same size.
% SORTFIELDS(A,'PARAMETER1','VALUE1','PARAMETER2','VALUE2',....) uses the
% properties:
% DIM selects a dimension along which to sort.
% MODE selects the direction of the sort
% 'ascend' results in ascending order
% 'descend' results in descending order
%
% EXAMPLES:
% >> A.field1 = [9,1,8,2,7,3,6,4,5]; A.field2 = fliplr(A.field1)
% A =
% field1: [9 1 8 2 7 3 6 4 5]
% field2: [5 4 6 3 7 2 8 1 9]
% >> B = sortfields(A)
% B =
% field1: [1 2 3 4 5 6 7 8 9]
% field2: [1 2 3 4 5 6 7 8 9]
% >> B = sortfields(A,'sortbyfield','field1')
% B =
% field1: [1 2 3 4 5 6 7 8 9]
% field2: [4 3 2 1 9 8 7 6 5]
% >> B = sortfields(A,'sortbyfield','field1','mode','descend','dim',2)
% B =
% field1: [9 8 7 6 5 4 3 2 1]
% field2: [5 6 7 8 9 1 2 3 4]

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
24 Nov 2008 Shlomi

This function is just what I looked for. Thanks.
I suggest you add another option to sort the fields according to a supplied variable and not only according to one of the fields.
It is a complimentary option to 'SORTBYFIELD' and can be called 'SORTBYVAR'.

24 Nov 2008 Shlomi

It'll be also useful if you return the parameter 'I' in the same manner that the sort function does. 'I' should be an array or cell array in the case that 'SORTBYFIELDS' was not used.

26 Sep 2009 Matt Fetterman

Seems quite useful !

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
structures Ian Howat 22 Oct 2008 10:15:58
cell arrays Ian Howat 22 Oct 2008 10:15:58
field Ian Howat 22 Oct 2008 10:15:58
sort Ian Howat 22 Oct 2008 10:15:58
structure Ian Howat 22 Oct 2008 10:15:58
utilities Ian Howat 22 Oct 2008 10:15:58
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com