简体   繁体   English

MATLAB:从另一个M文件中调用M文件

[英]MATLAB: Calling a M file from another M file

Sorry this is such a basic question, but I can't find a straight answer and it doesn't seem to have been answered on here before (perhaps because it is so basic?!) 对不起,这是一个基本的问题,但我找不到一个直接的答案,之前似乎没有回答过(也许是因为它是如此基本?!)

I'm trying to call a M function from within another M file. 我正试图从另一个M文件中调用M函数。 The function I am calling is a primary function and has no inputs or outputs: it is simply some lines of code that I'd like to insert many times in my main M-file. 我调用的函数是一个主函数,没有输入或输出:它只是一些代码行,我想在我的主M文件中多次插入。

The function is called 该函数被调用

function generateGrating

and resides in the file generateGrating.m. 并驻留在generateGrating.m文件中。 The main function is called 主函数被调用

function project

and resides in the file project.m. 并驻留在project.m文件中。 As you can see, there is no input or output in either. 如您所见,两者中都没有输入或输出。 They simply run their routines. 他们只是运行他们的惯例。

I have tried the following to attempt to call the function, with no luck: 我尝试了以下尝试调用该函数,没有运气:

generateGrating()
generateGrating
generateGrating.m()
generateGrating.m
generateGrating();
generateGrating;
generateGrating.m();
generateGrating.m;

Any help would be appreciated! 任何帮助,将不胜感激! It seems that the answer must be so basic that I can't find it anywhere :( 似乎答案必须是如此基本,以至于我无法在任何地方找到它:(

I think that you want to use a script, not a function . 我认为你想要使用脚本而不是函数

A script is a .m file without the keyword function at the beginning. 脚本是一个开头没有关键字功能的.m文件。

So for instance, i fI have two files: 例如,我有两个文件:

sub.m: sub.m:

b=b+1;

main.m: main.m文件:

function main
  b=1;
  b
  sub;
  b
  sub;
  b

I get the answer: 我得到了答案:

b=1
b=2
b=3

If I change sub.m to make it a function: 如果我改变sub.m使其成为一个函数:

sub.m: sub.m:

function sub
  b=b+1;

The variable b inside sub is now from a different scope than b in main.m So I get the answer: sub中的变量b现在来自main.m中与b 不同的范围所以我得到了答案:

b=1
b=1
b=1

It should just be generateGrating . 它应该只是generateGrating The files generateGrating.m and project.m need to both be in directories that are included in the Matlab path. 文件generateGrating.mproject.m需要包含在Matlab路径中的目录中。 The easiest way to accomplish that is to have them both be in the same directory and to run with that directory as your working directory. 实现这一目标的最简单方法是让它们都在同一目录中,并以该目录作为工作目录运行。 The simplest thing to do is to open project.m and hit F5 to run it, and click the "Change directory" button if it pops up. 最简单的方法是打开project.m并按F5运行它,然后单击“更改目录”按钮,如果它弹出。

NOTE 注意

If generateGrating is populating some variables (it sounds like that's what you're doing), then DON'T make generateGrating a function . 如果generateGrating正在填充一些变量(听起来就像你正在做的那样),那么不要让generateGrating成为一个function Otherwise any variables that are set in generateGrating.m will only be in scope within that function. 否则,在generateGrating.m中设置的任何变量都只在该函数的范围内。 For example: 例如:

% generateGrating.m
function generateGrating()
x = 42;

% project.m
x = 1;
generateGrating
disp(x)

Will display x = 1 because x is only 42 within the scope of the function generateGrating . 将显示x = 1,因为x在函数generateGrating的范围内仅为42。 But changing generateGrating.m to 但是将generateGrating.m更改为

% generateGrating.m
x = 42;

And running project again will display x = 42; 再次运行project将显示x = 42;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM