创建MATLAB函数 [英] Create functions in matlab

查看:244
本文介绍了创建MATLAB函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建与MATLAB的函数,所以我可以把它在我的code任何地方?

How can I create a function with MATLAB so I can call it any where in my code?

我是新来的MATLAB所以我会写code我想在MATLAB写的一个PHP的例子!

I'm new to MATLAB so I will write a PHP example of the code I want to write in MATLAB!

    Function newmatlab(n){
    n=n+1;
    return n;
    }
array=array('1','2','3','4');
foreach($array as $x){
$result[]=newmatlab($x);
}
print_f($result);

所以,概括地说,我需要循环数组,这数组中的函数应用到每个项目。

So in nutshell, I need to loop an array and apply a function to each item in this array.

有一个人能告诉我,MATLAB写上面的函数,所以我可以更好地了解?

Can some one show me the above function written in MATLAB so I can understand better?

请注意:我需要这个,因为我写了一个code,用于分析视频文件,然后绘制在图上的数据。然后我和保存到Excel和JPG这个图。我的问题是,我有200多个视频进行分析,所以我需要自动执行此code到循环内的文件夹和内部分析每个* .avi文件等。

Note: I need this because I wrote a code that analyzes a video file and then plots data on a graph. I then and save this graph into Excel and jpg. My problem is that I have more than 200 video to analyze, so I need to automate this code to loop inside folders and analyze each *.avi file inside and etc.

推荐答案

正如其他人所说,该文件彻底覆盖了本pretty,但也许我们可以帮助你理解。

As others have said, the documentation covers this pretty thoroughly, but perhaps we can help you understand.

有,你可以在Matlab定义函数的方式屈指可数,但可能让你上手最有用的是定义一个在M文件。我会用你的榜样code。您可以通过创建一个名为 newmatlab.m 在项目的目录下的文件,看起来像这样做到这一点。

There are a handful of ways that you can define functions in Matlab, but probably the most useful for you to get started is to define one in an m-file. I'll use your example code. You can do this by creating a file called newmatlab.m in your project's directory that looks something like this

% newmatlab.m
function result = newmatlab(array)
result = array + 1

请注意,该函数具有相同的名称,该文件并没有明确的收益语句 - 它的数字说出来由您指定的输出参数是什么( S)(结果在这种情况下)。

Note that the function has the same name as the file and that there is no explicit return statement - it figures that out by what you've named the output parameter(s) (result in this case).

然后,在同一目录下,你可以创建一个脚本(或其他功能),通过该名称叫你的 newmatlab 功能:

Then, in the same directory, you can create a script (or another function) that calls your newmatlab function by that name:

% main.m (or whatever)
a = [1 2 3 4];
b = newmatlab(a)

这就是它!这是一个简单的解释,但希望足以让你开始后的文档可以帮助更多的。

That's it! This is a simplified explanation, but hopefully enough to get you started and then the documentation can help more.

PS:有在Matlab中没有有;在M-文件中定义的电流路径,任何功能都可见。你可以找到的路径使用路径命令是什么。粗略地说,这将包含

PS: There's no "include" in Matlab; any functions that are defined in m-files in the current path are visible. You can find out what's in the path by using the path command. Roughly, it's going to consist of


  1. Matlab的自己的目录

  2. 您的文档目录的子目录MATLAB

  3. 当前工作目录

这篇关于创建MATLAB函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆