使用MATLAB执行文件或调用其文件放在另一个文件夹中的函数?

时间:2020-11-27 16:00:49

Tried Googling, but couldn't find anything.
I have a few files and folders in my current MATLAB folder.
One of those folders is called 'Map' and it has a 'map1.m' file which I want to call from my code in the current MATLAB folder.
In my code, I can't call it like this:

尝试谷歌搜索,但找不到任何东西。我当前的MATLAB文件夹中有一些文件和文件夹。其中一个文件夹称为“Map”,它有一个'map1.m'文件,我想从当前MATLAB文件夹中的代码调用它。在我的代码中,我不能这样称呼它:

/Map/map1;

but I can do so like this:

但我可以这样做:

cd Map;map1;cd ..;

Somehow the above method seems incorrect. Is there a more elegant way to do it?

不知何故,上述方法似乎不正确。有更优雅的方式吗?

3 个解决方案

#1


You can run the file without adding the folder to your path manually, using the run command, which is specifically for such cases. From the documentation:

您可以使用run命令手动运行该文件,而无需手动将文件夹添加到路径中,该命令专门用于此类情况。从文档:

run is a convenience function that runs scripts that are not currently on the path.

run是一个便捷函数,它运行当前不在路径上的脚本。

You call your function/script as

你将你的函数/脚本称为

run /Map/map1 

If you want to run the function/script by merely entering its name and not with the full (or relative) path, then you should add the folder to your path.

如果只想输入其名称而不是完整(或相对)路径来运行函数/脚本,则应将该文件夹添加到路径中。

As noted by @mutzmatron, you cannot use run to call functions with input/output arguments. So, unless if it's a script/function without input/output arguments, using run will not work and you'll have to add the folder to your path.

正如@mutzmatron所指出的,你不能使用run来调用带有输入/输出参数的函数。因此,除非它是没有输入/输出参数的脚本/函数,否则使用run将无法工作,您必须将文件夹添加到路径中。


EDIT

Just as a matter of good coding practice, and to work in cases where your function has inputs/outputs, adding/removing the folder from your path is the correct way to go. So for your case,

就像良好的编码实践一样,并且在您的功能具有输入/输出的情况下工作,从路径添加/删除文件夹是正确的方法。所以对于你的情况,

addpath /Map...map1;...rmpath /Map

The important thing is that your function call is sandwiched between the addpath and rmpath commands. If you have functions of the same name in both folders, then you should sandwich it tighter i.e., a line before and a line after, so as to avoid conflicts.

重要的是你的函数调用夹在addpath和rmpath命令之间。如果你在两个文件夹中都有相同名称的功能,那么你应该将它更紧密地夹在一起,即之前的行和后面的行,以避免冲突。

#2


Just add all those directories to the Matlab path with addpath like gnovice suggests. Then you'll be able to call the functions normally, and they'll be visible to which(), help(), depfun(), and the other Matlab meta-programming commands. You can put the addpath() calls in your startup.m file to have them automatically appear each time you start Matlab.

只需使用像gnovice建议的addpath将所有这些目录添加到Matlab路径。然后,您将能够正常调用这些函数,并且它们对于(),help(),depfun()和其他Matlab元编程命令都是可见的。您可以将addpath()调用放在startup.m文件中,以便在每次启动Matlab时自动显示它们。

Changing the path with addpath/map1()/rmpath each time has some drawbacks.

每次使用addpath / map1()/ rmpath更改路径都有一些缺点。

  • It's a performance hit because you're adding path manipulation to each call.
  • 这是一个性能损失,因为您正在为每个调用添加路径操作。

  • Functions in different directories won't be able to see each other.
  • 不同目录中的函数将无法相互查看。

  • It'll be harder to write and debug functions because the path context in which they execute will change dynamically, and won't be the same as what you see when you're in the editor and the base workspace.
  • 编写和调试函数会更难,因为它们执行的路径上下文将动态更改,并且与您在编辑器和基础工作区中看到的内容不同。

  • You need additional error handling code to make sure the path is properly restored if the called function errors out.
  • 您需要其他错误处理代码,以确保在被调用函数出错时正确恢复路径。

  • This won't work with the Matlab Compiler, if you want to deploy this code at some point.
  • 如果您想在某个时候部署此代码,则无法使用Matlab编译器。

And using run() or cd() yourself is ugly, because relative paths are going to have problems.

使用run()或cd()本身很难看,因为相对路径会有问题。

If you really want to separate the functions in the subdirectories so they can't "see" each other, you can make those directories namespaces by putting a "+" in front of their names, and then qualify the function calls with the namespace, like Map.map1().

如果你真的想要分离子目录中的函数,以便它们不能相互“看到”,你可以通过在它们的名字前放一个“+”来创建那些目录命名空间,然后使用命名空间限定函数调用,像Map.map1()。

#3


Just to contribute to the path-altering debate...

只是为改变路径的辩论做出贡献......

One way to make it a bit "safer" is to write

让它有点“安全”的一种方法是写

% start of my code: create function handles % to the functions I need:try   cd Map   map1_func = @map1;catch mexceptionendcd ..

This tries to preserve the current directory, and you get a handle to the function in a different directory.

这会尝试保留当前目录,并在不同的目录中获取该函数的句柄。

Only thing is, this method won't work if map1 relies upon other functions in the Map directory.

唯一的问题是,如果map1依赖于Map目录中的其他函数,则此方法将不起作用。

#1


You can run the file without adding the folder to your path manually, using the run command, which is specifically for such cases. From the documentation:

您可以使用run命令手动运行该文件,而无需手动将文件夹添加到路径中,该命令专门用于此类情况。从文档:

run is a convenience function that runs scripts that are not currently on the path.

run是一个便捷函数,它运行当前不在路径上的脚本。

You call your function/script as

你将你的函数/脚本称为

run /Map/map1 

If you want to run the function/script by merely entering its name and not with the full (or relative) path, then you should add the folder to your path.

如果只想输入其名称而不是完整(或相对)路径来运行函数/脚本,则应将该文件夹添加到路径中。

As noted by @mutzmatron, you cannot use run to call functions with input/output arguments. So, unless if it's a script/function without input/output arguments, using run will not work and you'll have to add the folder to your path.

正如@mutzmatron所指出的,你不能使用run来调用带有输入/输出参数的函数。因此,除非它是没有输入/输出参数的脚本/函数,否则使用run将无法工作,您必须将文件夹添加到路径中。


EDIT

Just as a matter of good coding practice, and to work in cases where your function has inputs/outputs, adding/removing the folder from your path is the correct way to go. So for your case,

就像良好的编码实践一样,并且在您的功能具有输入/输出的情况下工作,从路径添加/删除文件夹是正确的方法。所以对于你的情况,

addpath /Map...map1;...rmpath /Map

The important thing is that your function call is sandwiched between the addpath and rmpath commands. If you have functions of the same name in both folders, then you should sandwich it tighter i.e., a line before and a line after, so as to avoid conflicts.

重要的是你的函数调用夹在addpath和rmpath命令之间。如果你在两个文件夹中都有相同名称的功能,那么你应该将它更紧密地夹在一起,即之前的行和后面的行,以避免冲突。

#2


Just add all those directories to the Matlab path with addpath like gnovice suggests. Then you'll be able to call the functions normally, and they'll be visible to which(), help(), depfun(), and the other Matlab meta-programming commands. You can put the addpath() calls in your startup.m file to have them automatically appear each time you start Matlab.

只需使用像gnovice建议的addpath将所有这些目录添加到Matlab路径。然后,您将能够正常调用这些函数,并且它们对于(),help(),depfun()和其他Matlab元编程命令都是可见的。您可以将addpath()调用放在startup.m文件中,以便在每次启动Matlab时自动显示它们。

Changing the path with addpath/map1()/rmpath each time has some drawbacks.

每次使用addpath / map1()/ rmpath更改路径都有一些缺点。

  • It's a performance hit because you're adding path manipulation to each call.
  • 这是一个性能损失,因为您正在为每个调用添加路径操作。

  • Functions in different directories won't be able to see each other.
  • 不同目录中的函数将无法相互查看。

  • It'll be harder to write and debug functions because the path context in which they execute will change dynamically, and won't be the same as what you see when you're in the editor and the base workspace.
  • 编写和调试函数会更难,因为它们执行的路径上下文将动态更改,并且与您在编辑器和基础工作区中看到的内容不同。

  • You need additional error handling code to make sure the path is properly restored if the called function errors out.
  • 您需要其他错误处理代码,以确保在被调用函数出错时正确恢复路径。

  • This won't work with the Matlab Compiler, if you want to deploy this code at some point.
  • 如果您想在某个时候部署此代码,则无法使用Matlab编译器。

And using run() or cd() yourself is ugly, because relative paths are going to have problems.

使用run()或cd()本身很难看,因为相对路径会有问题。

If you really want to separate the functions in the subdirectories so they can't "see" each other, you can make those directories namespaces by putting a "+" in front of their names, and then qualify the function calls with the namespace, like Map.map1().

如果你真的想要分离子目录中的函数,以便它们不能相互“看到”,你可以通过在它们的名字前放一个“+”来创建那些目录命名空间,然后使用命名空间限定函数调用,像Map.map1()。

#3


Just to contribute to the path-altering debate...

只是为改变路径的辩论做出贡献......

One way to make it a bit "safer" is to write

让它有点“安全”的一种方法是写

% start of my code: create function handles % to the functions I need:try   cd Map   map1_func = @map1;catch mexceptionendcd ..

This tries to preserve the current directory, and you get a handle to the function in a different directory.

这会尝试保留当前目录,并在不同的目录中获取该函数的句柄。

Only thing is, this method won't work if map1 relies upon other functions in the Map directory.

唯一的问题是,如果map1依赖于Map目录中的其他函数,则此方法将不起作用。