开发者

Use a property of a classdef in another .m file?

Here is my code:

f.m:

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:');
dis开发者_运维技巧p(zeros);
disp('The Result:');
disp(z(t));
disp('The Initial Value:');
disp(z(0));
disp('The Final Value:');
disp(z(Inf));

When I type test in the command window, it tells me the following:

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


As Alex points out, you need an instance of f to access the member property x, like so:

myf = f();
f.x

You do not need an accessor method to get at x as it is defined as a public property. If you chose to make x private, then you'd need an accessor method something like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜