MATLAB: How to resolve between @spectrum/psd.m and psd.m

built-in-functionclass methodresolve ambiguity

Hi,
In Spectral Analysis http://www.mathworks.com/help/toolbox/signal/ug/f12-6587.html, under Nonparametric Methods>> Periodogram, the following code snippet is shown
fs = 1000; % Sampling frequency
t = (0:fs)/fs; % One second worth of samples
A = [1 2]; % Sinusoid amplitudes (row vector)
f = [150;140]; % Sinusoid frequencies (column vector)
xn = A*sin(2*pi*f*t) + 0.1*randn(size(t));
psd(Hs,xn,'Fs',fs,'NFFT',1024,'SpectrumType','onesided')
I am curious on which function psd is, so I used which to locate it:
>> which psd
C:\Program Files\MATLAB\R2008a\toolbox\signal\signal\psd.m
>> which spectrum.psd
C:\Program Files\MATLAB\R2008a\toolbox\signal\signal\@spectrum\psd.m % static method or package function
It seems that “which psd” returns a standalone function, whereas “which spectrum.psd” returns a static function of the spectrum class.
In standard Object-oriented programming language like C++, compiler examines argument list to resolve overloaded functions. But here,
Standalone psd.m;
function [Pxx, Pxxc, f] = psd(varargin)
@spectrum\psd.m:
function varargout = psd(this,x,varargin)
both take variable input argument lists, and the calling statement
psd(Hs,xn,'Fs',fs,'NFFT',1024,'SpectrumType','onesided')
has no return argument which, if length ≥4, could possibly be used to resolve ambiguity since standalone psd.m has maximum of 3 outputs.
So how could the compiler determine which psd.m is executed, the standalone one or the spectrum class method under @spectrum?
Bob

Best Answer

For
...
Hs = spectrum.periodogram;
psd(Hs,xn,'Fs',fs,'NFFT',1024,'SpectrumType','onesided')
MATLAB knows to call the spectrum.psd method sicne the first argument is of class spectrum.
I don't have access to MATLAB right now, but if instead you do
...
eval('Hs = spectrum.periodogram;');
psd(Hs,xn,'Fs',fs,'NFFT',1024,'SpectrumType','onesided')
I am pretty sure you confuse the JIT and you get the psd function.