Использовать свойство classdef в другом файле .m? - PullRequest
1 голос
/ 07 марта 2011

Вот мой код:

фм:

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 Ответ

3 голосов
/ 07 марта 2011

Как указывает Алекс, вам нужен экземпляр f для доступа к свойству элемента x, например:

myf = f();
f.x

Вам не нужен метод доступа, чтобы получить значение x, так как оно определено как открытое свойство. Если вы решили сделать x приватным, вам понадобится метод доступа, похожий на этот:

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