Passing Transforming function to step method of matlab.system.System

Goal: Apply the transform object T to the input data x, in a specefic manner as class A states.
Description: I have a class which does a specefic analyis called T, and another class which want to deploy an object from T on the data, x, in a specific order (class A). A is inherited from matlab.system.System because I can develop my code easier using "step Method" and blah blah. Step method just takes variables and fi objects but not function_handle or a external object (btw why? is it possible in general?). So I import the T object to the A.Func property and pass it to step method. In step method I call the A.Func.do to perform my desired T analysis. I couldn't find any other way :( (I'm newbie). But i constantly get a weird error which I don't understand.
Analysis class T:
classdef T < handle
properties
N
end
methods
% CONSTRUCTOR:
function obj = T(n)
if nargin == 1
obj.N = n;
else
obj.N = 16;
end
end
function y = do(x)
y = abs(fft(x,obj.N));
end
end
end
Structering Class A:
classdef A < matlab.system.System
properties
W % numeric properties
Func % function handle or object
end
methods
% constructor
function obj = A(Func,w)
if nargin == 2
obj.W = w;
obj.Func = Func;
else % you can use default assining in the properties instead...
obj.W = 16;
obj.Func = @(x) sin(x);
end
end
end
methods (Access=protected)
function y = stepImpl(obj,x)
y = obj.Func.do(x)*obj.W;
end
function numIn = getNumInputsImpl(~)
numIn = 1;
end
end
end
and the main script is:
clear all
clc
x = linspace(0,10*pi,100);
a = T(100);
b = A(T,3);
step(b,x)
but I get this error:
Error using T/do
Too many input arguments.
Error in A/stepImpl (line 30)
y = obj.Func.do(x)*obj.W;
Error in Main (line 9)
step(b,x)
Class T Code:
Class A Code:

 Réponse acceptée

Sadid
Sadid le 26 Déc 2012
Modifié(e) : Sadid le 26 Déc 2012
Hi, Marry Christmass,
The problem was solved by a technique called object composition. Just for documenting, in the case of static method "." operator should be used. and with the superclass "@" operator. (and bunch of concepts about superiority/inferiority) ... however using direct call (some sort of overloading) was enough to have a working class:
Class A:
classdef A < matlab.system.System
properties
W % numeric properties
Func % function handle or object
end
methods
function obj = A(Func,w)
if nargin == 2
obj.W = w;
obj.Func = Func;
else
obj.W = 16;
obj.Func = @(x) sin(x);
end
end
end % method
methods (Access=protected)
function y = stepImpl(obj,x)
y = do(obj.Func,x)*obj.W;
end
function numIn = getNumInputsImpl(~)
numIn = 1;
end
end
end
Class T:
classdef T < handle
properties
N
end
methods
function obj = T(n)
if nargin == 0
obj.N = 16;
end
if nargin == 1
obj.N = n;
end
end
function y = do(obj,x)
y = abs(fft(x,obj.N));
end
end
end % method
Main Script:
clear all
clear classes
clc
x = linspace(0,10*pi,100);
a = T(100);
b = A(T,3);
step(b,x)

1 commentaire

Sadid
Sadid le 26 Déc 2012
Modifié(e) : Sadid le 26 Déc 2012
Just for curosity I have problem with this implementation and I can't find the reason, your explanation certainly help, thanks
Cass A:
classdef A < matlab.system.System
properties
W % numeric properties
Func % function handle or object
end
methods
% constructor
function obj = A(Func,w)
if nargin == 2
obj.W = w;
obj.Func = Func;
else
obj.W = 16;
obj.Func = @(x) sin(x);
end
end
end % method
methods (Access=protected)
function y = stepImpl(obj,x)
y = step(obj.Func,x)*obj.W;
end
function numIn = getNumInputsImpl(~)
numIn = 1;
end
end
end
Class T:
classdef T < handle & matlab.system.System
%%Properties
properties
N
end
%%Methods
methods
% CONSTRUCTOR:
function obj = T(n)
if nargin == 0
obj.N = 16;
end
% obj = obj@handle(n); % it is not possible, why?
if nargin == 1
obj.N = n;
end
end
end
methods (Access = protected)
function y = stepiml(obj,x)
y = abs(fft(x,obj.N));
end
function numIn = getNumInputsImpl(~)
numIn = 1;
end
function numIn = getNumOutputsImpl(~)
numIn = 1;
end
end
end % method
with the just the above main script, I get this error:
Error using T/step
The stepImpl method needs to be defined for System objects that return outputs (getNumOutputs > 0).
Error in A/stepImpl (line 29)
y = step(obj.Func,x)*obj.W;
Error in Main (line 10)
step(b,x)
I can't understand this error :(. Would someone please describe the reason in this case?

Connectez-vous pour commenter.

Plus de réponses (1)

Hi, I'm not quite sure what you are trying to do in terms of defining your own System object. I'll give you a very simple example of how to write a System object class file with one property and a step() method.
This System object will be called ExpVarBase, it is designed to take a base value with a default of exp(1) and the step method takes a scalar or vector input and raises the base to each element of the input vector.
classdef ExpVarBase < matlab.System
% h = ExpVarBase
properties (Nontunable)
Base = exp(1);
end
methods
function obj = ExpVarBase(varargin)
setProperties(obj,nargin,varargin{:});
end
end
methods (Access=protected)
function y = stepImpl(obj, x)
y = obj.Base.^x;
end
end
end
You can use this file like this.
H = ExpVarBase; % use default base
input = 1i*linspace(0,4*pi,2000);
y = step(H,input);
xval = input./1i;
plot(xval,real(y),xval,imag(y));
Or
H = ExpVarBase('Base',2);
y = step(H,2); % same as 2^2

1 commentaire

Sadid
Sadid le 24 Déc 2012
Modifié(e) : Sadid le 24 Déc 2012
Thank you Wayne,
I have to perform complicated analysis on the data and not a simple function which can be easily manged by "function_handle". So I like to pass my available T class (which perform pretty complicated task)to A for being applied on x. So passing object from class T to the class A is crucial. I hope I've shown my point.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by