matlab OOP how to transfer value from properties to methods ?

2 visualizaciones (últimos 30 días)
tomer polsky
tomer polsky el 25 de Jun. de 2018
Comentada: Adam el 27 de Jun. de 2018
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 ...

Respuestas (2)

OCDER
OCDER el 25 de Jun. de 2018
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
  1 comentario
tomer polsky
tomer polsky el 26 de Jun. de 2018
I did what you said how ever I cant still see my varbile c . Not in the workspace and not in command window

Iniciar sesión para comentar.


Steven Lord
Steven Lord el 26 de Jun. de 2018
>> 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 comentarios
OCDER
OCDER el 27 de Jun. de 2018
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
Adam el 27 de Jun. de 2018
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Software Development Tools en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by