matlab OOP how to transfer value from properties to methods ?
이전 댓글 표시
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 ...
답변 (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
tomer polsky
2018년 6월 26일
OCDER
2018년 6월 26일
Do you have to use OOP? Matlab was designed to hide all that complex OOP from normal users. I think OOP was a recently added feature to satisfy software engineers. To learn OOP, try learning JAVA or C++. Both are useful language to use with matlab too.
tomer polsky
2018년 6월 26일
Adam
2018년 6월 26일
Classes can be quite easy to learn if you give yourself manageable examples. What is a lot less easy to learn, even with 10+ years of experience, is exactly how to design your classes in terms of how they interact with each other. That is the same in any language, although each has their quirks. Matlab OOP has some irritating features certainly, but I have been happily using it for the last 5 years and even though I still write some code in standalone functions, of course, I certainly would never go back now. I am more of a software engineer by trade, but still didn't really get my head around the general concepts of OOP very well prior to using it in Matlab.
So I certainly would not discourage the use of classes, it depends how much time you have to learn and whether you are able to pick things up quickly or not.
Even after many years of doing it I still find myself having created a class that in no way justifies its existence and is really just a convoluted structure for what could have been one or two standalone functions.
OCDER
2018년 6월 26일
I use OOP very sparingly in matlab since there are often faster methods that matlab was designed for.
And why can't you make a folder with those 10 separate functions? I've made a 300-function software with Matlab... all works well. Key is to group your functions according to some sort of theme. See example folder structure:
Folder structure
-Main
runMain.m
---DataAnalysis
-----Statistics
calcValues.m
-----Simulation
simulateProtein.m
---IO
formatFile.m
---Tables
ProteinProperties.csv
I've done OOP and it can become messy with improper design... Spaghetti code is fixed by coding practice. The fact you have 10 functions is actualy a good thing since you're segmenting your code , which makes it easy to debug. Try debugging a 300-line object code - it's not pleasant.
And COMMENT your codes!
example:
function A = calcSum(X, Y, Z)
%calcSum will calculate the sum of X, Y, and Z.
% A = calcSum(X, Y, Z)
% INPUT: X, Y, Z are scalars
% OUTPUT: sum of inputs
A = X + Y + Z;
That way, you can do "help calcSum" to figure out how to use that function.
Guillaume
2018년 6월 26일
To concur with what others have said. OOP is in no way a cure for spaghetti code, so switching to OOP for that reason is not a good idea.
That's coming from somebody who breathe and live through OOP having cut my teeth on C++ and later C# and who much prefer writing function objects in matlab to avoid all the silliness of name-value argument parsing that is the staple of regular functions.
tomer polsky
2018년 6월 27일
Adam
2018년 6월 27일
If you think classes can help you understand your code then they can be useful. You can use classes on many different levels. One of my colleagues once created a class that contained nothing but public static functions and there were 20 or more of them. As something of a purist this horrified me, but it did at least group together these 20 functions under a single umbrella. Using a package would do the same, though still leave them in 20 individual files. It's mostly a matter of what works for you.
When you have been working with a project for a while and are wanting to use OOP classes often just suggest themselves from the way you want to structure the code and they just flow naturally as containers for related functions (functions that act on the same variables).
I use classes in quite complicated ways nowadays because I am used to them, but you can use them simply to still be effective for code design and to hold the state of parts of a system.
Guillaume
2018년 6월 27일
As something of a purist this horrified me
I guess it depends on your background. In C#, for example, everything has to be in classes so having a class full of static functions (the class itself can be declared static) is not unusual.
@tomer, You can write messy classes just as easily as you can write messy non-OOP code, so don't think that OOP is going to automatically clean up your code. It's possible that thinking in OOP is going to be more natural for you and therefore result in cleaner code but that's not a given.
If the issue is one of organisation, you can divide functions into packages. However, my personal opinion is that packages are ill-designed in matlab so I don't use them anymore. Instead you can just divide your functions in subfolders (without the + prefix).
But by all means, if you want to use OOP, do.
tomer polsky
2018년 6월 27일
Start with functions. If you find yourself passing the same variable to lots of functions then that is an indication that it may be worth creating a class and combining all those functions, together with that variable (and any other relevant ones) into a class. But that isn't necessary and is best done only if you feel comfortable with it.
When I created my first Matlab class I had a nice self-contained problem that I decided I would try using a class to solve, rather than a complex program that would need me to design lots of classes working together. I moved onto that after I successfully created my first class.
It all depends how easy you find it to think in terms of what 'objects' should be present in your program. Sometimes it is easy, other times it is not at all. Ultimately only you will know for your own case.
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.
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!