Mercurial > octave-nkf
annotate scripts/miscellaneous/orderfields.m @ 19631:db92e7e28e1f
strip trailing whitespace from most source files
* NEWS, doc/interpreter/contributors.in, doc/interpreter/func.txi,
doc/interpreter/genpropdoc.m, doc/interpreter/octave_logo.eps,
doc/interpreter/plot.txi, doc/interpreter/stmt.txi,
examples/data/Makefile.am, libinterp/corefcn/data.cc,
libinterp/corefcn/debug.cc, libinterp/corefcn/error.cc,
libinterp/corefcn/file-io.cc, libinterp/corefcn/gl-render.cc,
libinterp/corefcn/graphics.cc, libinterp/corefcn/graphics.in.h,
libinterp/corefcn/load-path.cc, libinterp/corefcn/pr-output.cc,
libinterp/corefcn/pt-jit.cc, libinterp/corefcn/strfind.cc,
libinterp/corefcn/toplev.cc, libinterp/corefcn/toplev.h,
libinterp/corefcn/urlwrite.cc, libinterp/corefcn/variables.cc,
libinterp/octave-value/ov-classdef.cc,
libinterp/octave-value/ov-classdef.h, libinterp/octave.cc,
libinterp/parse-tree/lex.h, libinterp/parse-tree/oct-parse.in.yy,
libinterp/parse-tree/pt-classdef.h, liboctave/system/file-ops.cc,
liboctave/system/oct-env.cc, m4/acinclude.m4,
scripts/deprecated/finite.m, scripts/deprecated/fmod.m,
scripts/deprecated/fnmatch.m, scripts/deprecated/luinc.m,
scripts/deprecated/octave_tmp_file_name.m, scripts/deprecated/syl.m,
scripts/deprecated/usage.m, scripts/general/inputParser.m,
scripts/general/interp1.m, scripts/general/interp2.m,
scripts/general/interp3.m, scripts/general/isequal.m,
scripts/general/private/__isequal__.m, scripts/geometry/voronoi.m,
scripts/image/image.m, scripts/image/imshow.m,
scripts/image/ind2rgb.m, scripts/linear-algebra/bandwidth.m,
scripts/linear-algebra/isbanded.m, scripts/miscellaneous/bzip2.m,
scripts/miscellaneous/cast.m, scripts/miscellaneous/copyfile.m,
scripts/miscellaneous/delete.m, scripts/miscellaneous/fullfile.m,
scripts/miscellaneous/getappdata.m, scripts/miscellaneous/gunzip.m,
scripts/miscellaneous/isappdata.m, scripts/miscellaneous/ls.m,
scripts/miscellaneous/mex.m, scripts/miscellaneous/movefile.m,
scripts/miscellaneous/orderfields.m, scripts/miscellaneous/recycle.m,
scripts/miscellaneous/rmappdata.m, scripts/miscellaneous/setfield.m,
scripts/miscellaneous/symvar.m, scripts/miscellaneous/tar.m,
scripts/miscellaneous/tmpnam.m, scripts/miscellaneous/unpack.m,
scripts/miscellaneous/ver.m, scripts/miscellaneous/what.m,
scripts/miscellaneous/xor.m, scripts/miscellaneous/zip.m,
scripts/optimization/fminbnd.m, scripts/optimization/sqp.m,
scripts/path/private/getsavepath.m, scripts/path/savepath.m,
scripts/pkg/pkg.m, scripts/pkg/private/installed_packages.m,
scripts/plot/draw/plotyy.m, scripts/plot/draw/polar.m,
scripts/plot/draw/private/__quiver__.m,
scripts/plot/draw/private/__scatter__.m,
scripts/plot/draw/private/__stem__.m, scripts/plot/draw/surface.m,
scripts/plot/draw/surfnorm.m, scripts/plot/util/copyobj.m,
scripts/plot/util/hgload.m, scripts/plot/util/hgsave.m,
scripts/plot/util/isprop.m, scripts/plot/util/linkprop.m,
scripts/plot/util/private/__go_draw_axes__.m, scripts/set/setdiff.m,
scripts/set/union.m, scripts/signal/periodogram.m,
scripts/sparse/eigs.m, scripts/sparse/ilu.m, scripts/sparse/qmr.m,
scripts/sparse/sprand.m, scripts/sparse/sprandn.m,
scripts/specfun/beta.m, scripts/specfun/ellipke.m,
scripts/specfun/isprime.m, scripts/statistics/base/lscov.m,
scripts/testfun/__run_test_suite__.m, scripts/testfun/test.m:
Strip trailing whitespace.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 20 Jan 2015 10:29:54 -0500 |
parents | 1fa328da03ef |
children | 4197fc428c7d |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17620
diff
changeset
|
1 ## Copyright (C) 2006-2013 Paul Kienzle |
5881 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5881 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5881 | 18 |
19 ## -*- texinfo -*- | |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
20 ## @deftypefn {Function File} {@var{sout}] =} orderfields (@var{s1}) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{sout}] =} orderfields (@var{s1}, @var{s2}) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{sout}] =} orderfields (@var{s1}, @{@var{cellstr}@}) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
23 ## @deftypefnx {Function File} {@var{sout}] =} orderfields (@var{s1}, @var{p}) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
24 ## @deftypefnx {Function File} {[@var{sout}, @var{p}] =} orderfields (@dots{}) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
25 ## Return a @emph{copy} of @var{s1} with fields arranged alphabetically, or as |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
26 ## specified by the second input. |
5881 | 27 ## |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
28 ## Given one input struct @var{s1}, arrange field names alphabetically. |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
29 ## |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
30 ## If a second struct argument is given, arrange field names in @var{s1} as they |
17620
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
31 ## appear in @var{s2}. The second argument may also specify the order in a |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
32 ## cell array of strings @var{cellstr}. The second argument may also be a |
19631
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19310
diff
changeset
|
33 ## permutation vector. |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
34 ## |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
35 ## The optional second output argument @var{p} is the permutation vector which |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
36 ## converts the original name order to the new name order. |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
37 ## |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
38 ## Examples: |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
39 ## |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
40 ## @example |
13930
fe088cef3503
doc: Add missing @group/@end group to orderfields docstring
Rik <octave@nomad.inbox5.com>
parents:
13929
diff
changeset
|
41 ## @group |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
42 ## s = struct ("d", 4, "b", 2, "a", 1, "c", 3); |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
43 ## t1 = orderfields (s) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11567
diff
changeset
|
44 ## @result{} t1 = |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
45 ## @{ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
46 ## a = 1 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
47 ## b = 2 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
48 ## c = 3 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
49 ## d = 4 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
50 ## @} |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
51 ## @end group |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
52 ## @group |
15878
6be613476e37
doc: orderfields.m bug #38009
Andreas Weber <andy.weber.aw@gmail.com>
parents:
14868
diff
changeset
|
53 ## t = struct ("d", @{@}, "c", @{@}, "b", @{@}, "a", @{@}); |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
54 ## t2 = orderfields (s, t) |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
55 ## @result{} t2 = |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
56 ## @{ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
57 ## d = 4 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
58 ## c = 3 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
59 ## b = 2 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
60 ## a = 1 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
61 ## @} |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
62 ## @end group |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
63 ## @group |
15878
6be613476e37
doc: orderfields.m bug #38009
Andreas Weber <andy.weber.aw@gmail.com>
parents:
14868
diff
changeset
|
64 ## t3 = orderfields (s, [3, 2, 4, 1]) |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
65 ## @result{} t3 = |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
66 ## @{ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
67 ## a = 1 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
68 ## b = 2 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
69 ## c = 3 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
70 ## d = 4 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
71 ## @} |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
72 ## @end group |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
73 ## @group |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 ## [t4, p] = orderfields (s, @{"d", "c", "b", "a"@}) |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
75 ## @result{} t4 = |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
76 ## @{ |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
77 ## d = 4 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
78 ## c = 3 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
79 ## b = 2 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
80 ## a = 1 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
81 ## @} |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
82 ## p = |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
83 ## 1 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
84 ## 4 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
85 ## 2 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
86 ## 3 |
13930
fe088cef3503
doc: Add missing @group/@end group to orderfields docstring
Rik <octave@nomad.inbox5.com>
parents:
13929
diff
changeset
|
87 ## @end group |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
88 ## @end example |
5881 | 89 ## |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
90 ## @seealso{fieldnames, getfield, setfield, rmfield, isfield, isstruct, struct} |
5881 | 91 ## @end deftypefn |
92 | |
93 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
94 ## Adapted-By: jwe | |
95 | |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
96 function [sout, p] = orderfields (s1, s2) |
5881 | 97 |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
98 if (nargin < 1 || nargin > 2) |
5881 | 99 print_usage (); |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
100 elseif (! isstruct (s1)) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
101 error ("orderfields: S1 must be a struct"); |
5881 | 102 endif |
103 | |
104 if (nargin == 1) | |
105 ## One structure: return the fields in alphabetical order. | |
6862 | 106 if (isstruct (s1)) |
5881 | 107 names = sort (fieldnames (s1)); |
108 endif | |
109 elseif (nargin == 2) | |
6862 | 110 if (isstruct (s2)) |
5881 | 111 ## Two structures: return the fields in the order of s2. |
112 names = fieldnames (s2); | |
113 if (! isequal (sort (fieldnames (s1)), sort (names))) | |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 error ("orderfields: structures S1 and S2 do not have the same fields"); |
5881 | 115 endif |
116 elseif (iscellstr (s2)) | |
117 ## A structure and a list of fields: order by the list of fields. | |
118 t1 = sort (fieldnames (s1)); | |
119 t2 = sort (s2(:)); | |
120 if (! isequal (t1, t2)) | |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
121 error ("orderfields: CELLSTR list does not match structure fields"); |
5881 | 122 endif |
123 names = s2; | |
124 elseif (isvector (s2)) | |
125 ## A structure and a permutation vector: permute the order of s1. | |
126 names = fieldnames (s1); | |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
127 t1 = 1:numel (names); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
128 t2 = sort (s2); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
129 t2 = t2(:)'; |
5881 | 130 if (! isequal (t1, t2)) |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
131 error ("orderfields: invalid permutation vector P"); |
5881 | 132 endif |
17620
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
133 names = names(s2); |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
134 else |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
135 error ("orderfields: second argument must be structure, cellstr, or permutation vector"); |
5881 | 136 endif |
137 endif | |
138 | |
17620
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
139 ## Corner case of empty struct |
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
140 if (isempty (names)) |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
141 sout = struct (); |
17620
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
142 p = []; |
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
143 endif |
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
144 |
5881 | 145 ## Find permutation vector which converts the original name order |
146 ## into the new name order. Note: could save a couple of sorts | |
147 ## in some cases, but performance isn't critical. | |
148 | |
149 if (nargout == 2) | |
17620
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
150 [~, oldidx] = sort (fieldnames (s1)); |
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
151 [~, newidx] = sort (names); |
5881 | 152 p = oldidx(newidx); |
153 endif | |
154 | |
155 ## Permute the names in the structure. | |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
156 if (isempty (s1)) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
157 ## Corner case of empty structure. Still need to re-order fields. |
8605
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
158 args = cell (1, 2 * numel (names)); |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
159 args(1:2:end) = names; |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
160 args(2:2:end) = {[]}; |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
161 sout = struct (args{:}); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
162 ## inherit dimensions |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
163 sout = resize (sout, size (s1)); |
6862 | 164 else |
8612
20d23d65cc84
fix & optimize orderfields
Jaroslav Hajek <highegg@gmail.com>
parents:
8605
diff
changeset
|
165 n = numel (s1); |
8605
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
166 for i = 1:numel (names) |
16656
cf84ea2ba2d3
require dynamic field names to be character strings
John W. Eaton <jwe@octave.org>
parents:
15878
diff
changeset
|
167 el = names{i}; |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
168 [sout(1:n).(el)] = s1(:).(el); |
8605
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
169 endfor |
8612
20d23d65cc84
fix & optimize orderfields
Jaroslav Hajek <highegg@gmail.com>
parents:
8605
diff
changeset
|
170 ## inherit dimensions |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
171 sout = reshape (sout, size (s1)); |
6862 | 172 endif |
5881 | 173 |
174 endfunction | |
8594
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
175 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
176 |
8605
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
177 %!shared a, b, c |
8594
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
178 %! a = struct ("foo", {1, 2}, "bar", {3, 4}); |
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
179 %! b = struct ("bar", 6, "foo", 5); |
8605
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
180 %! c = struct ("bar", {7, 8}, "foo", 9); |
8594
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
181 %!test |
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
182 %! a(2) = orderfields (b, a); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
183 %! assert (a(2).foo, 5); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
184 %! assert (a(2).bar, 6); |
8594
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
185 %!test |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
186 %! [a(2), p] = orderfields (b, [2 1]); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
187 %! assert (a(2).foo, 5); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
188 %! assert (a(2).bar, 6); |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
189 %! assert (p, [2; 1]); |
8594
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
190 %!test |
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
191 %! a(2) = orderfields (b, fieldnames (a)); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
192 %! assert (a(2).foo, 5); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
193 %! assert (a(2).bar, 6); |
8605
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
194 %!test |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
195 %! a(1:2) = orderfields (c, fieldnames (a)); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
196 %! assert (a(2).foo, 9); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
197 %! assert (a(2).bar, 8); |
8605
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
198 |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
199 %!test |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
200 %! aa.x = {1, 2}; |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
201 %! aa.y = 3; |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
202 %! aa(2).x = {4, 5}; |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
203 %! bb.y = {6, 7}; |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
204 %! bb.x = 8; |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
205 %! aa(2) = orderfields (bb, aa); |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
206 %! assert (aa(2).x, 8); |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
207 %! assert (aa(2).y{1}, 6); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
208 |
17620
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
209 ## Corner case of empty struct, bug #40224 |
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
210 %!assert (orderfields (struct ()), struct ()) |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
211 %!test |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
212 %! s(2,2).a = 1; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
213 %! s(1,1).b = 2; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
214 %! s = resize (s, [1 0]); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
215 %! s2 = orderfields (s, {"b", "a"}); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
216 %! assert (fieldnames (s2), {"b"; "a"}); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
217 %! assert (size_equal (s, s2)); |
17620
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
218 |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
219 ## Test input validation |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
220 %!error orderfields () |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
221 %!error orderfields (1,2,3) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
222 %!error <S1 must be a struct> orderfields (1) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
223 %!error <S1 and S2 do not have the same fields> |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
224 %! s1.a = 1; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
225 %! s2.b = 2; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
226 %! orderfields (s1, s2); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
227 %!error <CELLSTR list does not match structure fields> |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
228 %! s1.a = 1; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
229 %! orderfields (s1, {"b"}); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
230 %!error <invalid permutation vector P> |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
231 %! s1.a = 1; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
232 %! orderfields (s1, [2 1]); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
233 %!error <second argument must be structure, cellstr, or permutation vector> |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
234 %! s1.a = 1; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
235 %! orderfields (s1, ones (2,2)); |