Functions in MATLAB
As seen in the previous section, a script file can contain a locally declared function. Now, traditionally a function in MATLAB is defined more globally by creating it in its specified file. A file that contains a function only needs to fulfill following conditions:
- Only one parent function is allowed per file.
- The file name must be the same as the function name.
See the following example in which we create a function ‘geeks’ in a file named geeks.m. This function will return the URL of w3wiki.
Example 3:
Matlab
% MATLAB code for % function function y=geeks y= "https://www.w3wiki.org" ; end |
Output:
As it can be seen that the file name is same as the function name, and it is globally accessible.
Scripts and Functions in MATLAB
In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following:
- Script
- Live Script
- Function only file
- Class file
Now only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we shall compare the script and function files.