0

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

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.

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

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.

2
  • 5
    Welcome to Stack Overflow. This is a very basic question, which you could easily answer by reading the comprehensive Matlab documentation. I suggest you look there first and come back if you need more specific help. Commented Jan 27, 2012 at 19:23
  • i did read that before, but coundn't understand ! thats why i ask, is there a way to include the function in my file ? or i must create it on different file ? Commented Jan 27, 2012 at 20:37

1 Answer 1

1

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

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

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).

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: 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's own directory
  2. The MATLAB subdirectory of your Documents directory
  3. The current working directory
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much :), now i get it. it just didn't make sense to me where should i put this function file and how can i include it and how can i control return, thank you very much for your amazing answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.