MATLAB 重载加运算符

MATLAB Overload plus operator

我想在 MATLAB 中使用加号运算符(即 plus(a,b))创建一个函数,当用户传递两个字符串时,它们会连接在一起并显示为结果。但是,每次我检查这个时,我都会收到无法实现内置函数的错误。是否可以在 MATLAB 中执行此操作?如果可以,执行此操作的过程是什么?

感谢任何对此问题的帮助。

  1. 创建名为 @char

  2. 的目录
  3. 在该目录中放置一个类似于以下的函数:

    function c = plus(a,b)
    c = horzcat(a,b); %// if you want the result to be output
    disp(c) %// if you want the result to be displayed
    
  4. 确保@char目录的父目录在MATLAB路径上(或者是当前目录)。

  5. 使用函数

    >> 'abc' + 'def'
    abcdef
    ans =
    abcdef