Mercurial > octave
annotate scripts/set/private/validsetargs.m @ 27229:255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
* NEWS: Announce change.
* intersect.m: Add new calling form and explanation of "legacy" option to
docstring. Allow up to 4 inputs in input validation. Check for "legacy"
in input options and set variable optlegacy. Only use two-input form
of sort or sortrows when number of outputs of intersect function is greater
than 1. After calculation, change orientation of outputs as required based
on optlegacy. Add BIST tests to test output orientation and values.
* validsetargs.m: Modify to accept a variable number of option arguments as
last input (varargin). Use switch statement to validate possible options
including accepting "legacy".
author | Rik <rik@octave.org> |
---|---|
date | Wed, 10 Jul 2019 17:32:52 -0700 |
parents | 00f796120a6d |
children | 9f44123dc25b |
rev | line source |
---|---|
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
1 ## Copyright (C) 2000-2019 Paul Kienzle |
11523 | 2 ## Copyright (C) 2009-2010 Jaroslav Hajek |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
3 ## |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
4 ## This file is part of Octave. |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
5 ## |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
6 ## Octave is free software: you can redistribute it and/or modify it |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
7 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
8 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
9 ## (at your option) any later version. |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
10 ## |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
11 ## Octave is distributed in the hope that it will be useful, but |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
14 ## GNU General Public License for more details. |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
15 ## |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
16 ## You should have received a copy of the GNU General Public License |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
17 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
18 ## <https://www.gnu.org/licenses/>. |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
19 |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
20 ## Validate arguments for binary set operation. |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
21 |
27229
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
22 function [x, y] = validsetargs (caller, x, y, varargin) |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
23 |
19700
00e31f316a3a
Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
19697
diff
changeset
|
24 isallowedarraytype = @(x) isnumeric (x) || ischar (x) || islogical (x); |
00e31f316a3a
Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
19697
diff
changeset
|
25 |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
26 if (nargin == 3) |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
27 icx = iscellstr (x); |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
28 icy = iscellstr (y); |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
29 if (icx || icy) |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
30 if (icx && ischar (y)) |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
31 y = cellstr (y); |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
32 elseif (icy && ischar (x)) |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
33 x = cellstr (x); |
20438
e59a44fa74ff
union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents:
19700
diff
changeset
|
34 elseif (icy && isempty (x)) |
e59a44fa74ff
union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents:
19700
diff
changeset
|
35 x = {}; |
e59a44fa74ff
union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents:
19700
diff
changeset
|
36 elseif (icx && isempty (y)) |
e59a44fa74ff
union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents:
19700
diff
changeset
|
37 y = {}; |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
38 elseif (! (icx && icy)) |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
39 error ("%s: cell array of strings cannot be combined with a nonstring value", caller); |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
40 endif |
19700
00e31f316a3a
Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
19697
diff
changeset
|
41 elseif (! (isallowedarraytype (x) && isallowedarraytype (y))) |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 error ("%s: A and B must be arrays or cell arrays of strings", caller); |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
43 endif |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
44 elseif (nargin == 4) |
27229
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
45 for arg = varargin |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
46 switch (arg{1}) |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
47 case "legacy" |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
48 ## Accepted option, do nothing. |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
49 |
27229
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
50 case "rows" |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
51 if (iscell (x) || iscell (y)) |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
52 error ('%s: cells not supported with "rows"', caller); |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
53 elseif (! (isallowedarraytype (x) && isallowedarraytype (y))) |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
54 error ("%s: A and B must be arrays or cell arrays of strings", caller); |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
55 else |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
56 if (ndims (x) > 2 || ndims (y) > 2) |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
57 error ('%s: A and B must be 2-dimensional matrices for "rows"', caller); |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
58 elseif (columns (x) != columns (y) && ! (isempty (x) || isempty (y))) |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
59 error ("%s: number of columns in A and B must match", caller); |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
60 endif |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
61 endif |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
62 |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
63 otherwise |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
64 error ("%s: invalid option: %s", caller, byrows_arg); |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
65 |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
66 endswitch |
255f2681d224
intersect.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
67 endfor |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
68 endif |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
69 |
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
70 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
71 |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
72 |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
73 ## %!tests for function are in union.m |