Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/matlab-programming/matlab-validate-function-arguments

matlab-validate-function-arguments

Verified

Use 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 implicit-conversion pitfalls (size reshaping, class coercion, computed defaults), or when restricting, constraining, or validating function inputs — scalar vs vector enforcement, type rejection, size checking. Also when asked to harden inputs, make a function more robust, tighten input checking, or rewrite for safer input acceptance — even without "validation" or "arguments block" wording. Triggers on: arguments block, mustBe validators, name-value arguments, varargin, inputParser, nargin/narginchk, restrict input, type checking, scalar constraint, harden inputs, defensive programming. Not for: App Designer callbacks, Simulink mask parameters, class inheritance, or runtime validation outside function or property declarations.

references/examples/repeating-args.md

View on GitHub
# Repeating Arguments — Worked Examples

For the basic paired-data pattern (`plotMultiSeries`), see the "Repeating
Arguments" section of `SKILL.md`. The examples below extend that pattern.

## Three-Element Groups

Accept (x, y, lineSpec) triplets:

```matlab
function styledPlot(x, y, lineSpec)
    arguments (Repeating)
        x (1,:) double {mustBeFinite}
        y (1,:) double {mustBeFinite}
        lineSpec (1,1) string
    end

    figure;
    hold on;
    for i = 1:numel(x)
        plot(x{i}, y{i}, lineSpec{i});
    end
    hold off;
end
```

Call: `styledPlot([1 2 3], [4 5 6], "-r", [1 2 3], [7 8 9], "--b")`

## Two-Block Pattern: Repeating + Name-Value

Combine repeating groups with name-value options using two `arguments` blocks:

```matlab
function stackedBarChart(categories, values, options)
    arguments (Repeating)
        categories (1,:) string
        values (1,:) double {mustBeFinite}
    end
    arguments
        options.Title (1,1) string = ""
        options.ColorMap (1,1) string = "parula"
        options.Normalize (1,1) logical = false
    end

    numGroups = numel(categories);

    % Collect all unique categories
    allCats = categories{1};
    for i = 2:numGroups
        allCats = union(allCats, categories{i}, 'stable');
    end

    % Build data matrix
    dataMatrix = zeros(numel(allCats), numGroups);
    for i = 1:numGroups
        for j = 1:numel(categories{i})
            idx = find(allCats == categories{i}(j), 1);
            dataMatrix(idx, i) = values{i}(j);
        end
    end

    if options.Normalize
        colSums = sum(dataMatrix, 1);
        colSums(colSums == 0) = 1;
        dataMatrix = dataMatrix ./ colSums * 100;
    end

    figure;
    bar(categorical(allCats, allCats), dataMatrix, 'stacked');
    colormap(options.ColorMap);
    if options.Title ~= ""
        title(options.Title);
    end
end
```

Call: `stackedBarChart(["A","B","C"], [10,20,30], ["A","B","C"], [5,15,25], Title="Sales")`

## Key Rules

1. **All args in the repeating block repeat together** — you cannot have some repeat and others not
2. **Variables become cell arrays** — access with `{i}` indexing
3. **No defaults allowed** in repeating blocks
4. **Zero repetitions is valid** — produces 1x0 empty cell arrays
5. **Name-value args go in a separate block** — they cannot be in the same block as repeating args
6. **Only one repeating input block** per function
7. **Do NOT use `varargin`** with argument validation — declare named variables instead

## Repeating Outputs — What Fails to Parse

For the working `productTriples` example and the one-name rule, see the
"Output Argument Validation" section of `SKILL.md`. The counter-example below
shows the parse-time failure mode:

```matlab
% BAD — multi-name (Output, Repeating) — does not parse
function [p, a, b] = productTriples()
    arguments (Output, Repeating)
        p (1,1) double
        a (1,1) double
        b (1,1) double
    end
    % Error: Declaring multiple repeating output arguments is not supported.
    % Identifier: MATLAB:functionValidation:MultipleRepeatingOutputs
end
```

To return groups of related values, declare a single output and pack groups
into it via convention (e.g. every third output is the product, the next two
are factors). Validators on the single declared name apply to each cell
element; comma-list expansion at the call site distributes elements across
the requested LHS variables.

## Anti-Pattern: varargin in Repeating Block

This is syntactically valid but useless — it provides no validation benefit:

```matlab
% BAD — do not do this
function bad(varargin)
    arguments (Repeating)
        varargin
    end
    % varargin is still just a cell array with no validation
end
```

Instead, declare named variables with types and validators.

----

Copyright 2026 The MathWorks, Inc.

----
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 319kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 196kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 196kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 196kUpdated Claude CodeCodexVerifiedInstall