了解MATLAB类属性(Understanding MATLAB class properties)
从MATLAB帮助中考虑这个例子 。
这个例子除了有语法问题外,并不适用于我。 我不知道是否是版本问题,我正在使用R2013a。
classdef MyClass
properties (Constant = true)
X = pi/180;
end
properties
PropA = sin(X*MyClass.getAngle([1 0],[0 1]);
end
methods (Static = true)
function r = getAngle(vx,vy)
end
end
end
它说
未定义的函数或变量'X'。 MyClass(第1行)中的错误classdef MyClass
我可以通过添加MyClass.X来修复它,但我不知道这是否是目的。
Considering this example from the MATLAB Help.
This example, besides having syntax problems, does not seams to work for me. I don't know if is a version issue, I'm using R2013a.
classdef MyClass
properties (Constant = true)
X = pi/180;
end
properties
PropA = sin(X*MyClass.getAngle([1 0],[0 1]);
end
methods (Static = true)
function r = getAngle(vx,vy)
end
end
end
It says
Undefined function or variable 'X'. Error in MyClass (line 1) classdef MyClass
I can fix it by adding MyClass.X, but I don't know if this was the purpose.
原文:https://stackoverflow.com/questions/22057351
2020-07-10 21:07
满意答案
MathWorks的例子都搞砸了。 意图可能是这样写的:
classdef MyClass
properties (Constant = true)
Deg2Rad = pi/180;
end
properties
PropA = sin(MyClass.Deg2Rad*MyClass.getAngle([1 0],[0 1]));
end
methods (Static = true)
function r = getAngle(vx,vy)
r = atan2(vy,vx)/MyClass.Deg2Rad;
end
end
end
我想重点是演示一个静态方法和常量属性:
>> MyClass.getAngle(1,sqrt(3))
ans =
60.0000
>> MyClass.getAngle(sqrt(3),1)
ans =
30.0000
>> MyClass.getAngle(0,1)
ans =
90
That MathWorks example is all messed up. The intention was probably to have it written like this:
classdef MyClass
properties (Constant = true)
Deg2Rad = pi/180;
end
properties
PropA = sin(MyClass.Deg2Rad*MyClass.getAngle([1 0],[0 1]));
end
methods (Static = true)
function r = getAngle(vx,vy)
r = atan2(vy,vx)/MyClass.Deg2Rad;
end
end
end
I guess the point is to demonstrate a static method and constant property:
>> MyClass.getAngle(1,sqrt(3))
ans =
60.0000
>> MyClass.getAngle(sqrt(3),1)
ans =
30.0000
>> MyClass.getAngle(0,1)
ans =
90
2014-02-27
相关问答
此页面上有一条说明可以解释问题: 注意:仅在首次需要该值时才会对属性默认值进行评估,而在MATLAB首次初始化该类时仅进行一次。 每次创建类实例时,MATLAB都不会重新评估表达式。 我认为这意味着当您创建一个类实例时, m尚未初始化,因此您无法使用它来设置另一个属性n的默认值。 我能让它工作的唯一方法是将m声明为Constant属性: classdef myclass
properties (Constant = true)
m=2;
end
proper...
我认为代码实际上工作正常,只是没有做到你期望的。 你定义它的方式, foo是一个值类,因此它具有值语义,而不是引用(或句柄)语义。 执行转换changer(myobj) ,MATLAB正在创建一个myobj的副本,其中包含新值B并将其返回给您。 最初的myobj保持不变。 实现值类时,通常会向changer添加输出参数,以便能够进一步使用此新副本。 function obj = changer(obj)
如果将foo设置为句柄类,则继承自handle : classdef foo
...
一个更简单的解决方案是 axis off
但它带来了一个警告:它也会去除x和y标签。 所以如果你想包含标签并只删除数字,nibot的答案是正确的方法。 A simpler solution is axis off
but it comes with a caveat: it also removes the x and y labels. So if you wanted to include labels and remove only the numbers, nibot's answer...
一种可能性是重构create方法以返回三个值(而不是对属性进行硬编码),然后您可以在超类和子级中调用它: [sup.a,sup.b,sup.c] = sup.create(...);
和 [sub.a1,sub.b1,sub.c1] = sub.create(args2);
哪里 classdef superclass < handle
methods (Access = protected)
function [x,y,z] = create(obj, args)
...
这是一个工作示例: classdef ProjectTable
properties (GetAccess = ?AllProjectTables)
Value
end
methods
function obj = ProjectTable(val)
obj.Value = val;
end
function r = addTwo(obj)
r = obj.Val...
您遇到的问题是将句柄类对象设置为类属性的默认值 。 相关文件说明了这一点: MATLAB®仅在加载类时评估属性默认值。 每次创建该类的对象时,MATLAB都不会重新评估赋值。 如果在类定义中将对象指定为默认属性值,则MATLAB在加载类时仅调用该对象的构造函数一次。 因此,对于上面的Storage类, 所有实例都将使用存储在tree属性中的相同默认containers.Map对象。 而且由于containers.Map类是handle类的子类,因此它具有引用行为,这意味着对象的副本都将指向相同的底...
如果需要可选属性,可以从dynamicprops继承类。 然后,您可以使用addprop命令addprop添加属性,使用addprop测试属性是否存在,甚至通过侦听事件PropertyAdded和PropertyRemoved来响应要添加或删除的PropertyRemoved 。 在您的示例中,您将使用: classdef test_parser < matlab.mixin.Copyable & dynamicprops
properties (AbortSet = true)
...
摆脱这个问题很容易: classdef VehiclesHandle
必须 classdef VehiclesHandle < handle
要了解原因,请阅读句柄和价值类的比较 。 Getting rid of the problem is quite easy: classdef VehiclesHandle
has to be classdef VehiclesHandle < handle
And to understand why, please read Comparison ...
是的,您总是在类方法中将属性称为obj.width或obj.height 。 我认为你可能夸大了这里的低效率 - 如果你真的担心它,你至少可以轻松地将问题减少66%,使用名称o来引用你的对象而不是obj 。 Yes, you would always refer to the properties as obj.width or obj.height within the class methods. I think you're perhaps exaggerating the ineffici...
MathWorks的例子都搞砸了。 意图可能是这样写的: classdef MyClass
properties (Constant = true)
Deg2Rad = pi/180;
end
properties
PropA = sin(MyClass.Deg2Rad*MyClass.getAngle([1 0],[0 1]));
end
methods (Static = true)
function r...
相关文章
中文名: MATLAB及应用 作者: 胡鹤飞 图书分类: 软件 资源格式: PDF
...
1. 描述 尝试用多种方法读取属性文件。 测试打印系统属性; 测试读取、写入用户属性文件; 测试
...
中文名: MATLAB智能算法30个案例分析 作者: 史峰 王辉 郁磊 胡斐
...
中文名: 模式识别与智能计算:MATLAB技术实现(第2版) 作者: 杨淑莹 图书分类:
...
中文名: 数字图像处理与机器视觉:Visual C++与Matlab实现 作者: 张铮 图
...
Java类用于描述一类事物的共性,该类事物有什么属性,没有什么属性,至于这个属性的值是什么,则是由这个
...
我把log4j.properties文件放到"WEB-INF\classes"目录
...
If the configuration directory for a Solr core cont
...
.net的动态编译功能很好,但是有个问题,动态编译的代码,每次执行后,都会产生一个新的assembly
...
最新问答
如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re
第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型
这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;
问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是
我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar
Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/
你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV
12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar
这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定