$npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-validate-function-argumentsUse when writing MATLAB functions with arguments blocks — repeating arguments (arguments (Repeating)), .?ClassName property import in constructors, name-value forwarding with namedargs2cell, or migrating from inputParser or validateattributes. Also when reviewing signatures for i
| 1 | # MATLAB Function Argument Validation |
| 2 | |
| 3 | Write robust MATLAB functions using `arguments` blocks with correct semantics for size, class, repeating arguments, and property import. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - Writing a function with an `arguments` block |
| 8 | - Using repeating arguments (`arguments (Repeating)`) |
| 9 | - Writing a class constructor that accepts name-value arguments |
| 10 | - Migrating from `inputParser` or `validateattributes` |
| 11 | - Reviewing a function signature for implicit conversion pitfalls |
| 12 | - Choosing between class specs, `mustBeA`, and validators |
| 13 | |
| 14 | ## When NOT to Use |
| 15 | |
| 16 | - Basic MATLAB programming without argument validation |
| 17 | - App building or UI components (use `matlab-building-apps`) |
| 18 | - Unit testing (use `matlab-testing`) |
| 19 | - General OOP class design unrelated to argument validation |
| 20 | - Simple `if`/`error` guard clauses for runtime invariants inside a function body |
| 21 | (those aren't input validation — leave them as-is unless the user asks for an |
| 22 | `arguments` block specifically) |
| 23 | - **`.mlx` (Live Script) or `.mlapp` (App Designer) files** — these are binary |
| 24 | ZIP containers. Don't unzip them or attempt structural edits. If the user |
| 25 | asks to add an `arguments` block to a function inside one, ask them to |
| 26 | export to plain `.m` first, or open the file in MATLAB and edit it there. |
| 27 | |
| 28 | ## Critical Misconceptions |
| 29 | |
| 30 | These are things the agent commonly gets wrong. Read these FIRST. |
| 31 | |
| 32 | ### Size specs RESHAPE, not reject |
| 33 | |
| 34 | **WRONG belief:** `(1,:)` rejects column vectors with an error. |
| 35 | |
| 36 | **ACTUAL behavior:** MATLAB silently reshapes the input to fit the declared size. |
| 37 | |
| 38 | ```matlab |
| 39 | function out = myFunc(x) |
| 40 | arguments |
| 41 | x (1,:) double |
| 42 | end |
| 43 | out = x; |
| 44 | end |
| 45 | |
| 46 | myFunc([1; 2; 3]) % Does NOT error! Returns [1 2 3] (reshaped to row) |
| 47 | ``` |
| 48 | |
| 49 | A column vector `[1;2;3]` passed to `(1,:)` becomes a row vector `[1 2 3]`. To actually reject column vectors, use a validator: |
| 50 | |
| 51 | ```matlab |
| 52 | function out = myFunc(x) |
| 53 | arguments |
| 54 | x {mustBeNumeric, mustBeRow} |
| 55 | end |
| 56 | out = x; |
| 57 | end |
| 58 | ``` |
| 59 | |
| 60 | ### Class specs CONVERT, not reject |
| 61 | |
| 62 | **WRONG belief:** `double` in the arguments block rejects non-double inputs. |
| 63 | |
| 64 | **ACTUAL behavior:** MATLAB attempts implicit conversion to the declared class. |
| 65 | |
| 66 | ```matlab |
| 67 | function out = myFunc(x) |
| 68 | arguments |
| 69 | x double |
| 70 | end |
| 71 | out = x; |
| 72 | end |
| 73 | |
| 74 | myFunc('hello') % Does NOT error! Returns [104 101 108 108 111] (ASCII codes) |
| 75 | myFunc(single(3.14)) % Does NOT error! Returns double(3.14) |
| 76 | ``` |
| 77 | |
| 78 | To reject without converting, use `mustBeA` or `mustBeFloat`: |
| 79 | |
| 80 | ```matlab |
| 81 | x {mustBeA(x, "double")} % Rejects single, char, int32, etc. |
| 82 | x {mustBeFloat} % Accepts single OR double, rejects char/int |
| 83 | x {mustBeNumeric} % Accepts any numeric, rejects char/string |
| 84 | ``` |
| 85 | |
| 86 | ### Numeric validators don't reject complex values |
| 87 | |
| 88 | **WRONG belief:** `mustBeNumeric`, `mustBeFinite`, and `mustBeInteger` reject |
| 89 | complex inputs like `1+2i`. |
| 90 | |
| 91 | **ACTUAL behavior:** Complex values are numeric, finite, and (when their real |
| 92 | and imaginary parts are integer-valued) integer — so `1+2i` passes all three |
| 93 | silently. Sizes, indices, counts, and most physical scalars should reject |
| 94 | complex values explicitly: |
| 95 | |
| 96 | ```matlab |
| 97 | function H = hilb2(n) |
| 98 | arguments |
| 99 | n (1,1) {mustBeInteger, mustBePositive, mustBeReal} |
| 100 | end |
| 101 | H = 1./((1:n)' + (0:n-1)); |
| 102 | end |
| 103 | ``` |
| 104 | |
| 105 | Add `mustBeReal` whenever a complex input would be nonsensical for the |
| 106 | function's contract. |
| 107 | |
| 108 | ### Computed defaults see PASSED values, not declared defaults |
| 109 | |
| 110 | **WRONG belief:** Default expressions use the declared default values of earlier arguments. |
| 111 | |
| 112 | **ACTUAL behavior:** Default expressions evaluate using the actual passed values. |
| 113 | |
| 114 | ```matlab |
| 115 | function out = myFunc(fs, windowSize) |
| 116 | arguments |
| 117 | fs (1,1) double {mustBePositive} |
| 118 | windowSize (1,1) double {mustBePositive} = round(fs / 10) |
| 119 | end |
| 120 | out = windowSize |