How to overload user defined functions in Matlab?

后端 未结 1 1068
囚心锁ツ
囚心锁ツ 2020-12-31 13:10

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

function show_seq(seq)
 plot (seq)
end

I now want to overload this show_seq to sh

相关标签:
1条回答
  • 2020-12-31 14:02

    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.

    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
    
    0 讨论(0)
提交回复
热议问题