Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 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 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 15:13


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.