Most of the scripts files that one ultimately use will be function M files. These files again have the .m extensions, but they are used in a different way than scripts. Function files have input and output arguments, and behave like other programming subroutines like in C and Java languages.
The structure of a typical function file, say dummyfunction.m is as follows :
function outputs = dummyfunction(inputs)
code
.
..
…
Outputs = …
must note here that name of the .m file is same as the function name. Let’s make a file here :
Step 1 : make a matlab file
Create a dummyFunction.m file in the matlab directory, in which you are working on.
% function <output> = <function name>(<input parameters>) function y = dummyFunction(x) count = 0; % variable declaration and definition for i = 0: x % starting for loop count = count+1 end
Note that the word function appears at the start of the file, and in lower case letters.
Step 2 : Call that function from matlab.
>> dummyFunction(10) count = 1 count = 2 count = 3 count = 4 count = 5 count = 6 count = 7 count = 8 count = 9 count = 10 count = 11