Cannot find 'set' method for .... class

Matlab complains about a set method that has already been defined. The error message is, when I type in the command:
set(a, 'prop1', 1)
"Error using set. Cannot find 'set' method for aSimpleValueClass class".
classdef aSimpleValueClass
properties
prop1
end
methods
function obj = set.prop1(obj, x)
obj.prop1 = x;
end
end
end
Can someone tell me why?

 採用された回答

Walter Roberson
Walter Roberson 2017 年 12 月 15 日

1 投票

You would need to have defined a method named set for that case. The set.prop1 method is called for the case where you use
a.prop1 = value

8 件のコメント

Yves
Yves 2017 年 12 月 15 日
編集済み: Yves 2017 年 12 月 15 日
I thought (in the body of my classdef)
function obj = set.prop1(obj, value)
end
IS my set method, according to the OOP manual and examples.
per isakson
per isakson 2017 年 12 月 15 日
Walter Roberson
Walter Roberson 2017 年 12 月 15 日
When I look at the documentation, it appears to me you would need a method named just set in order to use set() as a call.
Yves
Yves 2017 年 12 月 15 日
Somehow I thought my syntax was correct based on this manual page (please click on the hyperlink):
The set method has a difference, I noticed though from the manual, between a value class and a handle class: "Value class set functions must return the modified object to the calling function. Handle classes do not need to return the modified object." However, I re-typed
a = set(a, 'prop1', 3.0);
did not make the error message go away.
per isakson
per isakson 2017 年 12 月 15 日
This is a better reference: Implement Set/Get Interface for Properties
Guillaume
Guillaume 2017 年 12 月 15 日
@Yves, no, you misunderstood. A property set method is used when you want to override what happens when you do
obj.property = value;
and it only works when you use that syntax. If you want to be able to set or get property with a generic set or get function, as works for graphics objects, you need to derive from matlab.mixin.SetGet which will automatically provide these methods. Note that matlab.mixin.SetGet forces your class to be a handle class, so the syntax would have to be
set(a, 'prop1', 3.0);
If you want do have a value class and a generic set method you'll have to write it yourself. The function will be defined as
methods
function obj = set(obj, property, value)
%code to set property, eg:
if isprop(obj, property)
obj.(property) = value;
end
end
end
The above function would still live alongside the set.prop1 function if you want to override what happens when assigning to prop1 (e.g. validate inputs)
Yves
Yves 2017 年 12 月 15 日
Ahh...I see. Thx, Guillaume.
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021 年 10 月 21 日
Also, thanks by me!
Best!

サインインしてコメントする。

その他の回答 (2 件)

sneha pc
sneha pc 2020 年 5 月 3 日

0 投票

error- set is not comaptable with sym class. what to do
M = 0.5;
m = 0.2;
b = 0.1;
I = 0.006;
g = 9.8;
l = 0.3;
q = (M+m)*(I+m*l^2)-(m*l)^2;
syms s
Ppend=((m*l*s^2)/q)/(s^4+(b*(I+m*l^2)*s^3)/q-((M+m)*m*g*l*s^2)/q-(b*m*g*l*s)/q);
disp(Ppend);
Pcart=(((I+m*l^2)*s^2-g*m*l)/q)/(s^4+(b*(I+m*l^2)*s^3)/q-((M+m)*m*g*l*s^2)/q-(b*m*g*l*s)/q);
disp(Pcart)
sys_tf = [P_cart ; P_pend];
inputs = {'u'};
outputs = {'x'; 'phi'};
set(sys_tf,'InputName',inputs)
set(sys_tf,'OutputName',outputs)
sys_tf;
Walter Roberson
Walter Roberson 2020 年 5 月 3 日

0 投票

You created a symbolic expression that looks like a transfer function, and you are expecting that it is suddenly the transfer function datatype instead of being symbolic.
You should use the routine in this link:
https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/26553/versions/2/previews/sym2tf.m/index.html

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by