在Matlab中,是否可以将文件夹中组织的类的方法放在子目录/子文件夹中?

huangapple go评论79阅读模式
英文:

In matlab, is it possible to put methods of classes organised in folders in subdirectories/subfolders?

问题

在Matlab中,我可以将类的方法放在子文件夹中吗?

英文:

I am new to the option of organising your classes in class folders in matlab. In terms of readability, my code benefits from putting methods of the class in separate files, because oftentimes the methods are quite lengthy. Now, I believe it would be even nicer, if I could further organise my code; I'd like to create subdirectories within the class folder. The structure I'm looking for is like this (just an example):

@Foo/
├──Foo.m/
│
├──staticMethods/
│  ├──staticMethod1.m
│  ├──staticMethod2.m
│
├──privateMethods/
│  ├──privateMethod1.m

However, I can't seem to find anything about subfoldering in class folders, nor does it work when I try it, because matlab seems not to find the folders staticMethods and privateMethods.

Therefore my question: in matlab class folders, is it possible to create subfolders with methods in them?

答案1

得分: 3

不可能。只有目录Foo本身会被搜索以查找M文件,它的子目录不会被搜索。例外情况是名为private的子目录,该子目录可以包含仅对其父目录中的M文件可见的M文件函数(文档)。

公共方法必须直接位于Foo目录中。私有方法可以位于private子目录中。静态方法必须在classdef文件内定义。

通常,您的目录结构可以如下所示:

@Foo/
├──Foo.m
├──A.m
├──B.m
├──private/
│  ├──X.m
│  ├──Y.m

Foo.m必须包含classdef定义类Foo(或者,如果您选择使用预classdef类,只需包含其构造函数方法,尽管这不推荐)。classdef定义包含所有静态方法、设置器和获取器以及需要特殊访问权限的任何方法。
函数ABFoo的公共方法。XY是可以被Foo.mA.mB.mX.mY.m中的任何代码访问的函数。


为了构建代码结构,您可以将较长的函数拆分为私有子函数,只在classdef中留下其声明:

classdef ClassName
   properties
      ...
   end
   methods
      function obj = C(obj,vararg)
         obj = C_internal(obj,vararg{:});
      end
   end

然后,在一个名为private/C_internal.m的文件中:

function obj = C_internal(obj,arg2,arg3,arg4,...)
   ...
end

(注意使用vararg,它可以防止重复参数列表,重复的参数列表是引入错误的常见方式。)

唯一的问题是文档仍然必须位于Foo.m中。个人而言,我不喜欢将代码与文档分开,更喜欢将它们放在同一个文件中。

英文:

No, this is not possible. Only the directory Foo itself is searched for M-files, its subdirectories are not. The exception is a subdirectory called private, which can contain M-file functions only visible to the M-files within its parent directory (documentation).

Public methods all have to be directly in the Foo directory. Private methods can be in the private subdirectory. Static methods must be defined within the classdef file.

In general, your directory structure can look like this:

@Foo/
├──Foo.m
├──A.m
├──B.m
├──private/
│  ├──X.m
│  ├──Y.m

Foo.m must contain the classdef definition for class Foo (or, if you go with a pre-classdef class, just its constructor method, though this is not recommended). The classdef definition contains all static methods, setters and getters, and any methods that need special access permissions.
Functions A and B are public methods of Foo. X and Y are functions accessible to any code within Foo.m, A.m, B.m, X.m and Y.m.


To structure your code, you can take longer functions and split them out as private sub-functions, leaving not much more than its declaration in the classdef:

classdef ClassName
   properties
      ...
   end
   methods
      function obj = C(obj,vararg)
         obj = C_internal(obj,vararg{:});
      end
   end

and in a file private/C_internal.m:

function obj = C_internal(obj,arg2,arg3,arg4,...)
   ...
end

(Note the use of vararg, which prevents repeating the argument list, duplicated argument lists are a good way of introducing bugs.)

The only issue here is that the documentation still has to sit in Foo.m. I personally don't like separating code from documentation, and prefer keeping them together in the same file.

huangapple
  • 本文由 发表于 2020年1月6日 23:52:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定