Strange problem with class defenition :: one object filed doesn't exist at all

1 次查看(过去 30 天)
Hi, I'm trying to write a simple class which handles the short-time window signals...I've a strange problem which I can't find out the cause:
the field stws is not created in the instanciated object and cause strange problems. My question is why instanciating object with or without input signal cause to elimination of this crucial part for example if you consider this test code:
close all clear all clear classes close all clc
n = 1:100;x = sin(n);a = ShortTimeWinSig(x);
this goes without error but you don't have any field, the object is empty and hence this command lead to error!!!! a.stws
Error:
Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack space can
crash MATLAB and/or your computer.
Error in ShortTimeWinSig/get.stws
The Code (ShortTimeWinSig.m):
classdef ShortTimeWinSig % this class handels short time windowed signal.
%%Properties
properties (GetAccess = public, SetAccess = public)
stws % the short-time windowed signal
winType = @hamming % window type
winLen % window length
end
properties (Dependent = true, SetAccess = private)
len
end
%%Methods
methods
% constructor
function obj = ShortTimeWinSig(x,windowHandle,windowLength)
if nargin>0
obj.stws = x;
if exist('windowHandle','var')
obj.winType = windowHandle;
else
obj.winType = @hamming;
end
if exist('windowLength','var')
obj.winLen = windowLength;
else
obj.winLen = floor(length(x)/8);
end
else
obj.stws = sin(1:100);
end
end
% designed such that the object is put on the output and
% automatically before this, the signal will be calculated again
% using the updated window parameter.
function y = get.stws(obj)
numSeg = ceil(obj.len/obj.winLen); % number of segments
if size(obj.stws,1)>1
origSig = obj.stws.';
origSig = origSig.';
else
origSig = obj.stws;
end
y = zeros(numSeg,obj.winLen);
for i=1:numSeg
y(i,:) = origSig((i-1)*obj.winLen+1:i*obj.winLen);
end
end % get.stws
function y = get.winType(obj)
if ~isa(obj,'ShortTimeWinSig')
error('the input argument should be "ShortTimeWinSig" object');
else
y = obj.winType;
end
end % get.winType
function y = get.len(obj)
if ~isa(obj,'ShortTimeWinSig')
error('the input argument should be "ShortTimeWinSig" object');
else
y = size(obj.stws,1)*size(obj.stws,2);
end
end % get.len
function y = get.winLen(obj)
if ~isa(obj,'ShortTimeWinSig')
error('the input argument should be "ShortTimeWinSig" object');
else
y = size(obj.winLen,1)*size(obj.winLen,2);
end
end % get.len
function obj = set.stws(obj,x)
if isa(x,'ShortTimeWinSig')
obj.stws = x.stws;
else
obj.stws = x;
end
end % set.stws
function obj = set.winType(obj,val)
if ~isa(obj,'ShortTimeWinSig')
error('the input argument should be "ShortTimeWinSig" object');
else
if ~isa(val,'function_handle')
error('the value assigned should be function hadle type')
else
obj.winType = val;
end
end
end % set.winType
function obj = set.len(obj,~)
fprintf( '%s%d\n' , 'signal length is: ',obj.len)
error( 'You cannot set Modulus explicitly');
end
function obj = set.winLen(obj,val)
if ~isa(obj,'ShortTimeWinSig')
error('the input argument should be "ShortTimeWinSig" object');
else
obj.winLen = val;
end
end % set.winLen
end
end

采纳的回答

Matt J
Matt J 2012-12-21
编辑:Matt J 2012-12-21
get.stws contains an expression obj.len and therefore calls get.len. At the same time, get.len contains an expression obj.stws and calls get.stws. This leads to an endless recursive loop.
Incidentally, code segments like this
if ~isa(obj,'ShortTimeWinSig')
error('the input argument should be "ShortTimeWinSig" object');
are unnecessary. There is no way an obj which is not of type ShortTimeWinSig can be passed to the get() methods of ShortTimeWinSig.
  1 个评论
Sadid
Sadid 2012-12-21
Thank you Matt,
You are absolutely right and ThANK YOU for the point and description of error mechanism. This was my first real OOP code, and pretty stupid!!. BTW I modify the class. Still there is some bad ideas here as you mentioned (isa as a case, is it realy matter to write get for every property?). I'll trying to make it better and hence learn more...
Thank you!, Thank you
classdef ShortTimeWinSig % this class handels short time windowed signal.
%%Properties
properties (GetAccess = public, SetAccess = public)
signal % the short-time windowed signal
winType = @hamming % window type
winLen % window length
end
properties (Dependent = true, SetAccess = private)
len
stws
end
%%Methods
methods
% constructor
function obj = ShortTimeWinSig(x,windowHandle,windowLength)
if nargin>0
if isa(x,'ShortTimeWinSig')
obj.signal = x.signal;
else
obj.signal = x;
end
if exist('windowHandle','var')
obj.winType = windowHandle;
else
obj.winType = @hamming;
end
if exist('windowLength','var')
obj.winLen = windowLength;
else
obj.winLen = floor(length(x)/8);
end
else
obj.signal = sin(1:100);
end
end
% designed such that the object is put on the output and
% automatically before this, the signal will be calculated again
% using the updated window parameter.
function y = get.stws(obj)
numSeg = ceil(obj.len/obj.winLen); % number of segments
y = zeros(numSeg,obj.winLen);
for i=1:numSeg-1
y(i,:) = obj.signal((i-1)*obj.winLen+1:i*obj.winLen);
end
y(end,1:(obj.len-(numSeg-1)*obj.winLen)) = obj.signal(end-(obj.len-(numSeg-1)*obj.winLen)+1:end);
end % get.stws
function y = get.winType(obj)
if ~isa(obj,'ShortTimeWinSig')
error('the input argument should be "ShortTimeWinSig" object');
else
y = obj.winType;
end
end % get.winType
function y = get.len(obj)
if ~isa(obj,'ShortTimeWinSig')
error('the input argument should be "ShortTimeWinSig" object');
else
y = length(obj.signal);
end
end % get.len
function y = get.winLen(obj)
if ~isa(obj,'ShortTimeWinSig')
error('the input argument should be "ShortTimeWinSig" object');
else
y = obj.winLen;
end
end % get.winLen
function obj = set.stws(obj,~)
error( ['You cannot set stws explicitly: ' num2str(obj.winLen)]);
end % set.stws
function obj = set.winType(obj,val)
if ~isa(obj,'ShortTimeWinSig')
error('the input argument should be "ShortTimeWinSig" object');
else
if ~isa(val,'function_handle')
error('the value assigned should be function hadle type')
else
obj.winType = val;
end
end
end % set.winType
function obj = set.len(obj,~)
fprintf( '%s%d\n' , 'signal length is: ',obj.len);
error( 'You cannot set Modulus explicitly');
end % set.len
function obj = set.winLen(obj,val)
if ~isa(obj,'ShortTimeWinSig')
error('the input argument should be "ShortTimeWinSig" object');
else
obj.winLen = val;
end
end % set.winLen
end
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Time-Frequency Analysis 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by