필터 지우기
필터 지우기

Call a function in an m-file from a method of a handle class

조회 수: 4 (최근 30일)
Ahmed Hossam
Ahmed Hossam 2017년 4월 6일
댓글: Ahmed Hossam 2017년 4월 6일
I would like to know, if the object, which is being created with the help of the (handle) class methods, would also be visible for modifications in a function called from a method of the (handle) class, even if I don't pass the object itself to that function?
If the object is visible to the function called in a class method, so I can just work with it from the function using it's name from the class itself (using the name of the "constructor function")?
Or must I pass the object to the function called in the class method?...
Thank you for your answer in advance!
  댓글 수: 1
Ahmed Hossam
Ahmed Hossam 2017년 4월 6일
편집: Ahmed Hossam 2017년 4월 6일
I just would like to have the Implementation of the class methods outside of the class so that the class looks clean and tidy without so many code lines inside it

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2017년 4월 6일
편집: Guillaume 2017년 4월 6일
A method operating on objects of a class must have the object as its first input in matlab. (In other languages, e.g C++, this first parameter is automatically added by the compiler).
However, you can separate the methods of a class into different files. See this page for the details. Basically, create a folder named @yourclassname, populate this folder with at least yourclassname.m which can just contain the class definition. You can implement the methods either in yourclassname.m or in separate files in the same @ folder. Either way, the first input of the method must be the class object.

추가 답변 (1개)

Adam
Adam 2017년 4월 6일
편집: Adam 2017년 4월 6일
If you don't pass the object in then it has to be a static function and has knowledge of the class only, not the specific object. You can define class functions in separate files, although I can't remember what you need to do, I've never wanted to do it myself. It is mentioned somewhere in the large pdf file on Matlab OOP though (Section 5-2 seems to cover this)
  댓글 수: 2
Ahmed Hossam
Ahmed Hossam 2017년 4월 6일
Is it weird to define each method of a class in a separate file? What is common practice here?
Guillaume
Guillaume 2017년 4월 6일
It's not weird. I do it sometimes when the class are complex. It is much easier to locate a particular file than scroll through 1000s of lines in a single file.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by