user defined and inbuilt function in Matlab programming language | applied electronics engineering

Latest

user defined and inbuilt function in Matlab programming language

By Applied Electronics - Monday, March 6, 2017 No Comments
Like any other programming language, Matlab programming language also supports functions. A function is a machine. You fed the machine with some input and the machine produces some output. Likewise function allows us to write program in understandable manner. It helps us in creating modular program. A large and complex program can be divided into smaller manageable programs using functions. See matlab code for qpsk modulation and demodulation and matlab code for qpsk.

We can say that there are two types functions in matlab. One is the inbuilt functions which produces some output if the function is supplied with required argument. And the other is the user defined function which we will show later afterwards.

Matlab Inbuilt functions


 Some example of inbuilt functions are eye(), sqrt(), rand(), randi(), plot() etc.

For example the eye(N,M) produces an identity matrix of given size. here eye is reserved keyword an also the function name. For example,

sqrt() calculates the square root of given input argument.
Another commonly used inbuilt function is the rand() function. It can take on different number of input arguments. If no input is supplied then it produces a random number between 0 and 1. For example,



User defined functions


The general syntax for user defined function in matlab programming language is,

          end

where the blue word function and end are keywords. A keyword is a reserved word which means you cannot use the same keyword for other purpose in matlab like for declaring variable. The function starts with reserved keyword function and ends with reserved keyword end. The keyword end is optional in the sense it depends on how you are using the function. It is required for nested functions.

The square bracket after the word function are output arguments which are optional. That is there might be one or multiple output argument and there might not be an output at all. Then we have an assignment operator followed by the function_name. function_name is user defined function name. A name that you give to the function but you cannot use reserved keywords like function. Then lastly there is the input argument list inside bracket( ).Within it you pass in input parameters according to the need of your function. We will now see some examples.

Let's say that you want to create a function that returns a 3x4 matrix with elements in the range 1 and 10. Then you would write the function in matlab programming language as follows,

Here myRand is the function name. As mentioned earlier, it is not required for a function to have an output name and input argument list. This user defined function myRand when used returns the required matrix when just using in command prompt of in some other matlab script. This is possible because we used the rand() function which creates the random matrix. The function must be ended using the keyword end.

When this function is used in command prompt we get the following,


In the function declaration the variable a is local to the function. It is only visible to the function. To ilustrate the significance of local variable of a function. Consider clearing up the matlab workspace. Until now the variable a is stored in matlab workspace but if we clear the matlab workspace then the local variable is also cleared and retyping a will not show up, that is matlab shows undefined variable.

To make a global variable that stores the function output we have to use the output argument like so,


Notice that here we did not use the end keyword because this is a simple function not a nested one.

Now the variable a, now the output of a function stores the result of the function call. Now we can make the function call as follows.




No Comment to " user defined and inbuilt function in Matlab programming language "