查看单个帖子
旧 2019-12-14, 20:13   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 在另一个.m文件中使用classdef的属性?

这是我的代码:

调频:

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 st; [nd] = numden(fx); % 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.

回答:

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

myf = f(); fx 由于x被定义为公共属性,因此不需要访问器方法即可到达x 。如果您选择将x设为私有,那么您将需要一个类似于以下内容的访问器方法:

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

更多&回答...
poster 当前离线   回复时引用此帖