参数传递 #

一、参数传递机制 #

1.1 值传递 #

MATLAB使用值传递(传值),函数内修改不影响原变量:

matlab
function result = modify_value(x)
    x = x * 2;
    result = x;
end

% 调用
a = 5;
b = modify_value(a);
fprintf('a = %d, b = %d\n', a, b);
% a = 5, b = 10(a未被修改)

1.2 模拟引用传递 #

使用返回值更新原变量:

matlab
function x = increment(x)
    x = x + 1;
end

% 调用
count = 0;
count = increment(count);  % 更新count

二、输入参数 #

2.1 必需参数 #

matlab
function result = power(base, exponent)
    result = base ^ exponent;
end

% 必须提供所有参数
y = power(2, 3);  % 8

2.2 可选参数 #

matlab
function result = process(data, method, threshold)
% PROCESS 处理数据
    
    % 设置默认值
    if nargin < 2 || isempty(method)
        method = 'mean';
    end
    if nargin < 3 || isempty(threshold)
        threshold = 0;
    end
    
    % 处理
    data = data(data > threshold);
    
    switch method
        case 'mean'
            result = mean(data);
        case 'sum'
            result = sum(data);
        case 'max'
            result = max(data);
        otherwise
            error('未知方法: %s', method);
    end
end

% 调用方式
r1 = process(data);              % 使用默认method和threshold
r2 = process(data, 'sum');       % 使用默认threshold
r3 = process(data, 'max', 10);   % 指定所有参数

2.3 名称-值参数 #

matlab
function result = analyze(data, varargin)
% ANALYZE 分析数据
    
    % 解析参数
    p = inputParser;
    addRequired(p, 'data', @isnumeric);
    addParameter(p, 'method', 'mean', @ischar);
    addParameter(p, 'normalize', false, @islogical);
    addParameter(p, 'verbose', false, @islogical);
    
    parse(p, data, varargin{:});
    
    % 获取参数值
    method = p.Results.method;
    normalize = p.Results.normalize;
    verbose = p.Results.verbose;
    
    % 处理
    if verbose
        fprintf('方法: %s, 归一化: %d\n', method, normalize);
    end
    
    result = compute(data, method);
    
    if normalize
        result = result / max(abs(result));
    end
end

% 调用
r = analyze(data, 'method', 'sum', 'normalize', true);
r = analyze(data, 'verbose', true);

2.4 varargin可变参数 #

matlab
function result = sum_all(varargin)
% SUM_ALL 计算所有输入的和
    
    result = 0;
    for i = 1:nargin
        val = varargin{i};
        if isnumeric(val)
            result = result + val;
        else
            warning('参数%d不是数值,已跳过', i);
        end
    end
end

% 调用
total = sum_all(1, 2, 3, 4, 5);  % 15

三、输出参数 #

3.1 单输出 #

matlab
function result = square(x)
    result = x^2;
end

3.2 多输出 #

matlab
function [mean_val, std_val, max_val] = stats(data)
% STATS 计算统计量
    
    mean_val = mean(data);
    std_val = std(data);
    max_val = max(data);
end

% 获取所有输出
[m, s, x] = stats(data);

% 获取部分输出
[m, ~] = stats(data);  % 只获取均值

3.3 varargout可变输出 #

matlab
function varargout = powers(x, n)
% POWERS 返回x的1到n次幂
    
    for i = 1:n
        varargout{i} = x^i;
    end
end

% 调用
[a, b, c] = powers(2, 3);  % a=2, b=4, c=8

3.4 条件输出 #

matlab
function [result, status] = divide(a, b)
% DIVIDE 安全除法
    
    if b == 0
        result = NaN;
        status = false;
    else
        result = a / b;
        status = true;
    end
end

% 调用
[r, s] = divide(10, 2);  % r=5, s=true
[r, s] = divide(10, 0);  % r=NaN, s=false

四、参数验证 #

4.1 类型检查 #

matlab
function result = process(data)
    if ~isnumeric(data)
        error('数据必须是数值类型');
    end
    
    if ~isvector(data)
        error('数据必须是向量');
    end
    
    result = data * 2;
end

4.2 范围检查 #

matlab
function result = clip(value, min_val, max_val)
    if value < min_val
        error('值不能小于%d', min_val);
    end
    if value > max_val
        error('值不能大于%d', max_val);
    end
    
    result = value;
end

4.3 inputParser #

matlab
function result = compute(data, varargin)
% COMPUTE 计算函数
    
    p = inputParser;
    
    % 必需参数
    addRequired(p, 'data', @(x) isnumeric(x) && ~isempty(x));
    
    % 可选参数
    addOptional(p, 'scale', 1, @isnumeric);
    
    % 名称-值参数
    addParameter(p, 'method', 'linear', @ischar);
    addParameter(p, 'tolerance', 1e-6, @isnumeric);
    addParameter(p, 'max_iter', 100, @(x) isnumeric(x) && x > 0);
    
    parse(p, data, varargin{:});
    
    % 使用参数
    data = p.Results.data * p.Results.scale;
    method = p.Results.method;
    tol = p.Results.tolerance;
    max_iter = p.Results.max_iter;
    
    % 处理...
    result = data;
end

4.4 arguments块(R2019b+) #

matlab
function result = process(data, options)
% PROCESS 处理数据
    
    arguments
        data (1,:) double
        options.Method (1,1) string = "mean"
        options.Threshold (1,1) double = 0
        options.Verbose (1,1) logical = false
    end
    
    if options.Verbose
        fprintf('方法: %s\n', options.Method);
    end
    
    data = data(data > options.Threshold);
    
    switch options.Method
        case "mean"
            result = mean(data);
        case "sum"
            result = sum(data);
    end
end

% 调用
r = process(data);
r = process(data, "Method", "sum", "Threshold", 10);

五、实用示例 #

5.1 配置参数处理 #

matlab
function result = analyze_data(data, varargin)
% ANALYZE_DATA 分析数据
    
    % 默认配置
    config = struct();
    config.method = 'mean';
    config.normalize = false;
    config.remove_outliers = false;
    config.outlier_threshold = 3;
    config.verbose = false;
    
    % 解析输入参数
    for i = 1:2:nargin-1
        field = varargin{i};
        value = varargin{i+1};
        if isfield(config, field)
            config.(field) = value;
        else
            warning('未知参数: %s', field);
        end
    end
    
    % 处理
    if config.verbose
        fprintf('开始处理...\n');
    end
    
    % 移除异常值
    if config.remove_outliers
        z = abs((data - mean(data)) / std(data));
        data = data(z < config.outlier_threshold);
    end
    
    % 归一化
    if config.normalize
        data = (data - min(data)) / (max(data) - min(data));
    end
    
    % 计算
    switch config.method
        case 'mean'
            result = mean(data);
        case 'median'
            result = median(data);
        case 'sum'
            result = sum(data);
    end
    
    if config.verbose
        fprintf('处理完成: %.4f\n', result);
    end
end

5.2 多返回值处理 #

matlab
function [result, info] = fit_curve(x, y, varargin)
% FIT_CURVE 曲线拟合
    
    % 解析参数
    p = inputParser;
    addRequired(p, 'x', @isnumeric);
    addRequired(p, 'y', @isnumeric);
    addParameter(p, 'degree', 2, @isnumeric);
    addParameter(p, 'plot', false, @islogical);
    parse(p, x, y, varargin{:});
    
    degree = p.Results.degree;
    do_plot = p.Results.plot;
    
    % 多项式拟合
    coeffs = polyfit(x, y, degree);
    y_fit = polyval(coeffs, x);
    
    % 计算误差
    residuals = y - y_fit;
    rmse = sqrt(mean(residuals.^2));
    r_squared = 1 - sum(residuals.^2) / sum((y - mean(y)).^2);
    
    % 结果
    result = y_fit;
    
    % 信息
    info.coefficients = coeffs;
    info.rmse = rmse;
    info.r_squared = r_squared;
    info.degree = degree;
    
    % 绘图
    if do_plot
        figure;
        plot(x, y, 'o', x, y_fit, '-');
        legend('原始数据', '拟合曲线');
        title(sprintf('R² = %.4f', r_squared));
    end
end

% 调用
[y_fit, info] = fit_curve(x, y, 'degree', 3, 'plot', true);
fprintf('RMSE: %.4f, R²: %.4f\n', info.rmse, info.r_squared);

六、最佳实践 #

6.1 参数顺序 #

matlab
% 推荐:必需参数在前,可选参数在后
function result = process(data, method, threshold)
    % ...
end

% 不推荐:可选参数在前
function result = process(threshold, data)
    % ...
end

6.2 默认值处理 #

matlab
% 推荐:使用isempty检查
function result = process(data, scale)
    if nargin < 2 || isempty(scale)
        scale = 1;
    end
    result = data * scale;
end

6.3 参数验证 #

matlab
% 推荐:尽早验证并返回错误
function result = process(data)
    if isempty(data)
        error('数据不能为空');
    end
    if ~isnumeric(data)
        error('数据必须是数值类型');
    end
    % 处理...
end

七、总结 #

本章学习了:

  1. 传递机制:值传递
  2. 输入参数:必需、可选、varargin
  3. 输出参数:单输出、多输出、varargout
  4. 参数验证:类型检查、范围检查、inputParser
  5. 最佳实践:参数顺序、默认值处理

下一章将学习匿名函数。

最后更新:2026-03-27