我在攻读电气工程本科学位时,MATLAB要求每个函数都必须定义在自己的文件中,即使它只有一行代码。
现在我正在攻读研究生学位,需要使用MATLAB编写一个项目。在新版本的MATLAB中,这个规定是否仍然存在?
如果可以将多个函数放入同一个文件中,那么是否有任何限制?例如,文件中的所有函数是否都可以从文件外部访问,还是只有与文件同名的函数可被访问?
注:我正在使用MATLAB R2007b版本。
我在攻读电气工程本科学位时,MATLAB要求每个函数都必须定义在自己的文件中,即使它只有一行代码。
现在我正在攻读研究生学位,需要使用MATLAB编写一个项目。在新版本的MATLAB中,这个规定是否仍然存在?
如果可以将多个函数放入同一个文件中,那么是否有任何限制?例如,文件中的所有函数是否都可以从文件外部访问,还是只有与文件同名的函数可被访问?
注:我正在使用MATLAB R2007b版本。
localfunctions
函数实现)。然而,我不建议养成使用此类技巧的习惯,因为可能有更好的选项来组织您的函数和文件。A.m
中有一个主函数A
,以及本地函数D
、E
和F
。现在假设您还有两个其他相关的函数B
和C
,分别在m文件B.m
和C.m
中,您也希望能够调用D
、E
和F
。这里有一些选项可供选择:
Put D
, E
, and F
each in their own separate m-files, allowing any other function to call them. The downside is that the scope of these functions is large and isn't restricted to just A
, B
, and C
, but the upside is that this is quite simple.
Create a defineMyFunctions
m-file (like in Jonas' example) with D
, E
, and F
as local functions and a main function that simply returns function handles to them. This allows you to keep D
, E
, and F
in the same file, but it doesn't do anything regarding the scope of these functions since any function that can call defineMyFunctions
can invoke them. You also then have to worry about passing the function handles around as arguments to make sure you have them where you need them.
Copy D
, E
and F
into B.m
and C.m
as local functions. This limits the scope of their usage to just A
, B
, and C
, but makes updating and maintenance of your code a nightmare because you have three copies of the same code in different places.
Use private functions! If you have A
, B
, and C
in the same directory, you can create a subdirectory called private
and place D
, E
, and F
in there, each as a separate m-file. This limits their scope so they can only be called by functions in the directory immediately above (i.e. A
, B
, and C
) and keeps them together in the same place (but still different m-files):
myDirectory/
A.m
B.m
C.m
private/
D.m
E.m
F.m
所有这些都有点超出了您的问题范围,可能比您需要的细节还要多,但我认为涉及到组织所有m文件的更一般关注点可能是有好处的。 ;)
通常来说,对于你的问题,答案是否定的,你不能在一个文件中定义多于一个外部可见的函数。不过,你可以返回对于本地函数的函数句柄,并且一个方便的方法是将其作为结构体字段。以下是一个示例:
function funs = makefuns
funs.fun1=@fun1;
funs.fun2=@fun2;
end
function y=fun1(x)
y=x;
end
function z=fun2
z=1;
end
这里是它的使用方法:
>> myfuns = makefuns;
>> myfuns.fun1(5)
ans =
5
>> myfuns.fun2()
ans =
1
我非常喜欢SCFrench的回答 - 我想要指出,可以使用assignin函数将这些函数直接导入工作区进行简单修改。(这样做让我想起了Python中“从y中导入x”的做法)
function message = makefuns
assignin('base','fun1',@fun1);
assignin('base','fun2',@fun2);
message='Done importing functions to workspace';
end
function y=fun1(x)
y=x;
end
function z=fun2
z=1;
end
然后像这样使用:
>> makefuns
ans =
Done importing functions to workspace
>> fun1(123)
ans =
123
>> fun2()
ans =
1
assignin('caller',...)
更为准确。您可能希望在另一个函数中使用这些函数。 - Cris Luengo与SCFrench的回答类似,但采用更符合C#风格的方式。
我会(并经常这样做)创建一个包含多个静态方法的类。例如:
classdef Statistics
methods(Static)
function val = MyMean(data)
val = mean(data);
end
function val = MyStd(data)
val = std(data);
end
end
end
作为静态方法,您不需要实例化该类。 调用函数的方式如下:data = 1:10;
mean = Statistics.MyMean(data);
std = Statistics.MyStd(data);
我使用Octave在一个.m文件中定义多个函数,然后从需要使用该文件中的函数的位置内部使用命令:
source("mycode.m");
octave:8> help source
'source' is a built-in function
-- Built-in Function: source (FILE)
Parse and execute the contents of FILE. This is equivalent to
executing commands from a script file, but without requiring the
file to be named `FILE.m'.
source
命令 :( - Martin Peckafunction [varargout] = main( subfun, varargin )
[varargout{1:nargout}] = feval( subfun, varargin{:} );
% paste your subfunctions below ....
function str=subfun1
str='hello'
截至R2017b版本,官方并不支持这种操作。相关文档中指出:
程序文件可以包含多个函数。如果文件只包含函数定义,则第一个函数是主函数,并且是MATLAB与文件名关联的函数。在主函数或脚本代码之后的函数称为本地函数。本地函数仅在文件内部可用。
然而,其他答案中提出的解决方法可以实现类似的功能。
我已经尝试使用Octave进行SCFRench和Ru Hasha测试。
最终它可以运行了:但是我做了一些修改。
function message = makefuns
assignin('base','fun1', @fun1); % Ru Hasha
assignin('base', 'fun2', @fun2); % Ru Hasha
message.fun1=@fun1; % SCFrench
message.fun2=@fun2; % SCFrench
end
function y=fun1(x)
y=x;
end
function z=fun2
z=1;
end
可以在其他的'm'文件中调用:
printf("%d\n", makefuns.fun1(123));
printf("%d\n", makefuns.fun2());
更新:
我添加了一个答案,因为在Octave中,无论是+72还是+20都不能正常工作。 我编写的代码完美地运行(上周五我测试过并在稍后写了这篇文章)。
^
,@idigas - embert