在另一个.m文件中使用classdef的属性?

时间:2011-03-07 04:11:04

标签: matlab matlab-class

这是我的代码:

f.m:<​​/ p>

classdef f < handle
    properties (Access = public)
        functionString = '';
        x;
    end
    methods
        function obj = f
            if nargin == 0
                syms s;
                obj.x = input('Enter your function: ');
                obj.functionString = ilaplace(obj.x);
            end
        end
        function value = subsref(obj, a)
            t = a.subs{:};
            value = eval(obj.functionString);
        end
        function display(obj)
        end
    end
end

test.m:

syms s t;
[n d] = numden(f.x); % Here I want to use x, which is the user input, How can I do such thing?
zeros = solve(n);
poles = solve(d);
disp('The Poles:');
disp(poles);
disp('The Zeros:');
disp(zeros);
disp('The Result:');
disp(z(t));
disp('The Initial Value:');
disp(z(0));
disp('The Final Value:');
disp(z(Inf));

当我在命令窗口中键入test时,它会告诉我以下内容:

>> test
??? The property 'x' in class 'f' must be accessed from a class instance because it
is not a Constant property.

1 个答案:

答案 0 :(得分:3)

正如Alex指出的那样,您需要一个f的实例来访问成员属性x,如下所示:

myf = f();
f.x

您不需要访问器方法来获取x,因为它被定义为公共属性。如果您选择将x设为私有,那么您需要一个类似于此的访问器方法:

function x = getX( obj )
  x = obj.x;
end