필터 지우기
필터 지우기

Storing functions in classdef files

조회 수: 6 (최근 30일)
Benjamin D'Anjou
Benjamin D'Anjou 2019년 5월 28일
댓글: Steven Lord 2019년 5월 28일
I have an object-oriented Matlab project with several classes. I store each class definition in a @-folder within a folder pathFolder (in Windows). For instance
pathFolder\@myClass\myClass.m
In the constructor of each class, I use custom-defined functions. For instance, here I would use myFunction to initialize a complicated property:
classdef myClass
properties
x;
y;
complicatedProperty;
end
methods
function obj = myClass(x,y)
obj.x = x;
obj.y = y;
obj.complicatedProperty = myFunction(x,y);
end
end
end
Right now myFunction is located in pathFolder along with the @-folder
pathFolder\@myClass\myClass.m
pathFolder\myFunction.m
That works. However, I only use myFunction within the constructor of myClass. To better organize my files, I would very much like to group myFunction.m with the class definition. However, I can't put it in the @-folder because .m files contained in the @-folder are not in the path and are automatically treated as methods of the class.
One option is to define myFunction as a static method of myClass, but I want to avoid that (it causes problems when I try to call superclass constructors in subclasses).
Another alternative is to define myFunction as a local function in myClass.m but I would also like to avoid that. I want to keep it in a separate file.
Is there a way to put the separate file myFunction.m in the @-folder so that it will be recognized in the Matlab path? I welcome suggestions.

채택된 답변

Guillaume
Guillaume 2019년 5월 28일
Private functions are ideal for this. Store the functions in a folder called private inside the @ folder. Any function inside the private folder is only visible to m files in the containing folder, without needing to be added to the path.
  댓글 수: 1
Benjamin D'Anjou
Benjamin D'Anjou 2019년 5월 28일
Works like a charm, thank you.

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

추가 답변 (2개)

Benjamin D'Anjou
Benjamin D'Anjou 2019년 5월 28일
In the end I decided to store both myFunction.m and the @-folder in another path folder that has the same name as the class:
pathFolder\myClass\@myClass\myClass.m
pathFolder\myClass\myFunction.m
Not exactly what I wanted, but it works well enough.

Steven Lord
Steven Lord 2019년 5월 28일
Another alternative is to define myFunction as a local function in myClass.m but I would also like to avoid that. I want to keep it in a separate file.
Why do you want to avoid this? If only one class uses this function, why not make it local to the class? I would likely write this as a class-related function at the end of the classdef file, after the end that closes the classdef block.
  댓글 수: 2
Benjamin D'Anjou
Benjamin D'Anjou 2019년 5월 28일
편집: Benjamin D'Anjou 2019년 5월 28일
Because it is easier for me to find my way in my code that way. That simple. It is incredibly painful to me to scroll down a long classdef file when I want to modify one of these local functions. I'd rather navigate folders.
Steven Lord
Steven Lord 2019년 5월 28일
There are tools that can help you navigate around your file. The "Go To Location in File" section of this documentation page lists many of these. I use bookmarks (set a bookmark in your current location and the helper, jump to the next bookmark to modify the class-related function, jump to the "next" bookmark which will wrap around) and the context menu "Open Selection" option to navigate around long class files quite a bit.

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

카테고리

Help CenterFile Exchange에서 Class File Organization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by