

Now if i want to generate the same functionality by using MATLAB function.i will simply go to the new option and select function. So now going to command window you can see that the file is successfully run and the result is shown in the command window.
Using functions in matlab code#
Now I want to run the code we will simply click on the RUN or we can also run the program by pressing F5 button. So i am writing here myscript.m and click on the save button now this file is saved. Now a dialog box will appear and select any directory where you want to save i am going to the default directory which is MATLAB in the documents folder and one important point is that i can save it by any name of my own choice. Now before running the scriptwe need to save this file so you simply go to the file and click save.

Now it is a simple code which is adding three numbers and multiplying the same numbers and saving the values in these two variables named as sum and product. Now the next command is the sum of these three variables so that is sum is equal to: So first of all i will initialize or declare the three variables and for example these three variables are: Now let’s suppose i want to add three numbers and multiply the same numbers. Generally, it is a good practice to write a couple of commands at the start of the file which are used to clear all the variables and the command window.The first one is clc which is used for clear screen and the second one is clear this command is used to clear all the variables from the workspace. So by clicking the script a blank file will be openedand in this blank file you can write your own code in any format allowed by MATLAB coding rules. I am using MATLAB 2021a so to generate a script you go on this option new and click this script. MATLAB script is the simplest form of the MATLAB file in which you can write your MATLAB code in any way allowed by the MATLAB rules.

Whenever you are going to perform a specific functionality by using MATLAB code you either go for a MATLAB script or a MATLAB function. Scripts and functions in Matlab- In this tutorial, i will be discussing the difference between script and function.
