matlab OOP how to transfer value from properties to methods ?
2 次查看(过去 30 天)
显示 更早的评论
Hello I am trying to learn progrmaing OOP in Matlab . How ever i dont have exprienct with this kind of procdiel working .
if for exmaple i have this code :
classdef OOP_101
properties
a=5
b=10
end
methods
function c=adding(obj)
c=a.obj+b.obj
end
end
end
but when I look for the value of variable c I get this eror :
Undefined function or variable 'c'.
of course this is just an exmaple ...
0 个评论
回答(2 个)
OCDER
2018-6-25
classdef OOP_101
properties
a=5
b=10
end
methods
function c=adding(obj)
c=obj.a + obj.b; %<=== you have it backwards. It's obj.a not a.obj.
end
end
end
Steven Lord
2018-6-26
>> myObj = OOP_101
myObj =
OOP_101 with properties:
a: 5
b: 10
>> c = adding(myObj)
c =
15
Note that I could have used whatever variable name I wanted as the output of the call to the adding method. I'm not required to call it c.
>> tomer = adding(myObj)
tomer =
15
In fact, I can store that output as an element in an array; I'm not limited to storing it in a scalar variable.
>> clear x
>> x(5) = adding(myObj)
x =
0 0 0 0 15
Creating your own classes in MATLAB is a bit of an advanced maneuver (it's in the Advanced Software Development section in the documentation) so if (as I suspect) you're new to MATLAB or it's been a while since you last used MATLAB I'd recommend starting with some of the more fundamental material, like the MATLAB Onramp course on the tutorials page.
Once you have a good foundation of the fundamentals of writing scripts and functions, to learn more about creating and working with classes I would read through and follow the examples on this documentation page, starting with the "Create a Simple Class" example.
13 个评论
OCDER
2018-6-27
Use OOP whenever you have to keep track of stored variables, and any of this applies:
1. Setting a property in the object should trigger updates for all the other properties.
EX: A = VideoReader('video.avi')
%Behind the scene, extract full path to this file, encoding format, etc.
2. Invoking a method of the object updates the variable
EX: A.readFrame;
%Behind the scene: get the frame, and UPDATE the current frame number
The one thing that functions do not work well with is tracking a set of internal variables over multiple function calls. Often, people use persistent variables in functions, but it's often confusing to use and could lead to issues.
Take a look at the VideoReader object. It has to keep track of the location of the video file, video encoding format, etc. Whenever you use a method of VideoReader, it then knows exactly what video you are referring to, and if needed, update what the current read frame is.
Adam
2018-6-27
And if you do choose to use OOP, here is the Matlab OOP 'bible':
though if you are using an earlier version of Matlab you should probably locate it via links from your Matlab version's documentation as it is updated with every version.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!