Welcome to the series of blogs on Digital Image Processing using MATLAB. If you are looking for complete guidance in understanding the concepts of the digital images and image processing using MATLAB, you’re at the right place! This blog/tutorial discusses the concepts and implementation of programs in MATLAB scripts. So let’s start!
A script is the simplest form of the MATLAB program. It contains a list of sequential commands and you can save it for later use. You can take several inputs to the script file and also generate and display multiple outputs from it. The variables generated during the execution of the program are stored in the workspace.
Creating a new script file
To create a new script file, click on the bottom of the + icon at the top left of your MATLAB window.
Click on the first option ‘script’ and you’ll find a new editor space ‘Untitled’ in the script window. You can also use the shortcut key ‘CNTR+N’ to open a new script window. The script file is saved in the current folder.
Before saving the script file, make sure that your current folder is set to the folder where you wish to save it. You can do this in two ways. Click the ‘Browse for folder’ icon shown in the following figure and browse to the folder. And the second option is to copy the address of the folder and paste it in the MATLAB address bar. Then press Enter to change the current folder.
The MATLAB script file has the extension .m e.g, my_script.m.
Input to a script file
You can take input to a script file using the variable assignment. There are three ways of assigning a variable in the script file.
Variable assignment within the script file
In this case, you assign the values to the input variables within the script file. For example,
variable1 = 10; variable2 = 20; variable3 = 25; variable_sum = variable1 + variable2 + variable3 variable_mult = variable1 * variable2 * variable3
You can save the above code as a script file. When you run the code, you’ll get the outputs variable_sum and variable_mult in the command window.
variable_sum = 55 variable_mult = 5000
To change the values of the input variables, re-assign them within the code.
Variable assignment in the command window
In this case, you assign the variables in the command window and in the script file you write the expressions only. For example, let’s assign the variables in the command window.
>> variable1 = 5; >> variable2 = 10; >> variable3 = 15;
And in the script write the expressions.
variable_sum = variable1 + variable2 + variable3 variable_mult = variable1 * variable2 * variable3
If you wish to change the values of the inputs, assign them in the command window and run the script again to get the updated outputs.
Using ‘input’ command
In this case, we use the input command to take input from the user in the command window. For example,
variable1 = input('Enter the first variable')
When the above command is executed, the text within the single quotes will be displayed in the command window. Then you enter the value and it is assigned to variable1. The complete script using the input command is as follows.
variable1 = input('Enter the first varible '); variable2 = input('Enter the second varible '); variable3 = input('Enter the third varible '); variable_sum = variable1 + variable2 + variable3 variable_mult = variable1 * variable2 * variable3
Note that you can use any message you wish. When you run the above program, you’ll get the first message in the command window,
Enter the first varible
Enter the first value and you’ll get the next message after pressing the enter key.
Enter the first varible 5 Enter the first varible
When you enter the last input, the program is executed and the outputs are displayed on the screen.
Enter the first varible 5 Enter the second varible 10 Enter the third varible 15 variable_sum = 30 variable_mult = 750
You can read the details of the input command here.
Outputs from a script file
The outputs variable_sum and variable_mult in the above examples are displayed in the command window because we didn’t use the semicolon. But this is not the standard way of displaying outputs. MATLAB has formal ways of displaying the outputs also. There are two methods for displaying an output: the disp command and the fprintf command.
the disp command:
This is the easiest and quick way to display the outputs. For example, the above program can be written using the disp command as follows.
variable1 = input('Enter the first variable '); variable2 = input('Enter the second variable '); variable3 = input('Enter the third variable '); variable_sum = variable1 + variable2 + variable3; variable_mult = variable1 * variable2 * variable3; disp(['The summation of the variables is ',num2str(variable_sum)]) disp(['The multiplication of the variables is ',num2str(variable_mult)])
The corresponding inputs and outputs on the command window will be as follows.
Enter the first variable 5 Enter the first variable 10 Enter the first variable 15 The summation of the variables is 30 The multiplication of the variables is 750
You can use the disp command in many ways according to the requirement. For example, like
- disp(variable_name): to display the variable only with no message.
- disp(‘The statement ‘): to display the statement only.
- disp([‘The statement’, num2str(variable_name)]): to display both the statement and the variable in the same line.
The command num2str is used to convert the numbers (datatype ‘double’) to the string (datatype ‘char’). This conversion is necessary because within one array similar types of variables can be concatenated.
The fprintf command:
This command provides a formatted way of displaying outputs. A typical statement using the fprintf command is,
fprintf(‘\n The statement before the variable %3.2f the statement after the variable’, variable_name)
The first argument is the statement part and the next argument is the variable used in the statement. \n indicates a new line. You can use \n anywhere in the text where you wish to start a new line.
%3.2f is the variable format string. % indicates the position of the variable. 3.2 indicates field width and precision after the decimal. So a maximum of 3 digits before the decimal point and a maximum of 2 digits after it are allowed.
f is the conversion characters (here the fixed-point notation), used to convert the numeric or character as text. A few conversion characters are listed in the following table.
So the program using the fprintf command is,
variable_sum = variable1 + variable2 + variable3; variable_mult = variable1 * variable2 * variable3; fprintf('\nThe summation of the variables is %3.1f',variable_sum) fprintf('\nThe multiplication of the variables is %3.1f \n',variable_mult)
The output will be the same as in the case of the disp command. The last \n is used so that the next command prompt starts from a new line. You can observe one of the advantages of the fprintf that no conversion of numbers to strings is required. Another advantage is that you can print as many variables along with their statements in one line command. For example, you can replace the two lines with one as,
fprintf('\nThe summation of the variables is %3.1f and the multiplication of the varibles is %3.1f \n',variable_sum,variable_mult)
The output will be
The summation of the variables is 30.0 and the multiplication of the varibles is 750.0
writing the variables in a file using fprintf
In addition to the displaying output, fprintf is also used for writing the outputs to the files like an excel sheet or a text file. You can go through the detailed description of the fprintf for writing variables to a file here.
The save command
You can save one or more variables that are available in the workspace. The variable are saved in a .mat file. The syntax is simple – save file_name or save(‘file_name’). When you run this command, all the variables in the workspace will be saved in the file_name.mat file. However, if you wish to save some specific variables only, the syntax would be,
save file\_name var1 var2 or save('file\_name','var1 ', 'var2')
So to save the variables in the above example, write the following command.
>> save output_vars variable_sum variable_mult
When you run the program, it will generate a file output_vars.mat in the current folder. You can retrieve the variables using the load command.
The load command
The load command retrieves the variables saved in the .mat file. So if you clear the variables from the workspace, the following command will retrieve the variables stored in the .mat file. You can use the command in both the script file or in the command window.
>> load output_vars
All the saved variables in the file will appear in the workspace.
In this tutorial, you have learned the concepts and implementation of a program in MATLAB scripts. In the next section, we will discuss MATLAB plots.
For training, Research Assistance, and Consultancy Services on Computer Vision, Deep Learning, Signal Processing, Image Processing, and Medical Signal/Image Processing.