如何在Matlab中重载用户定义的函数? [英] How to overload user defined functions in Matlab?

查看:106
本文介绍了如何在Matlab中重载用户定义的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制序列,我已经编写了一个函数

I am trying to plot sequences, I have written a function

function show_seq(seq)
 plot (seq)
end

我现在想重载show_seq以显示2个类似

I now want to overload this show_seq to show 2 sequences something like

function show_seq(seq1, seq2)
  plot(seq1,'color','r');
  plot(seq2, 'color', 'b');
end

但这不起作用,有人知道如何在MATLAB中重载函数吗?

but this does not work, does anyone have idea about how to overload functions in MATLAB?

推荐答案

如果将重载函数放在优先级较高的路径中,则可以重载自己的函数之一.有关路径优先级的更多详细信息,请参见此问题.

You can overload one of your own functions if you put the overloading function in a path that with higher precedence. For more details on path precedence, see this question.

但是,在您的情况下,最简单的方法是修改show_seq,以便它接受多个可选输入:

However, in your case, the easiest would be to modify show_seq so that it accepts multiple optional inputs:

function show_seq(varargin)
  hold on %# make sure subsequent plots don't overwrite the figure
  colors = 'rb'; %# define more colors here, 
                 %# or use distingushable_colors from the
                 %# file exchange, if you want to plot more than two

  %# loop through the inputs and plot
  for iArg = 1:nargin
      plot(varargin{iArg},'color',colors(iArg));
  end
end

这篇关于如何在Matlab中重载用户定义的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆