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:
采纳的回答
更多回答(1 个)
Wayne King
2012-12-24
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
类别
在 帮助中心 和 File Exchange 中查找有关 Create System Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!