MATLAB orientation course: MATLAB orientation course: Organized by Organized by FOCUS – R&D FOCUS – R&D Matlab Programming Matlab Programming Delivered by Delivered by A. K. Bhattacharyya A. K. Bhattacharyya Senior Project Assistant Senior Project Assistant Department of Electrical Engineering Department of Electrical Engineering IIT Kharagpur IIT Kharagpur
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
• Write a series of MATLAB statements into a file and then execute them with a single command.
• Write your program in an ordinary text editor (Notepad), giving the file a name of filename.m. The term you use for filename becomes the new command that MATLAB associates with the program. The file extension of .m makes this a MATLAB M-file.
• Scripts, on the other hand, do not have a separate workspace. They store their variables in a workspace that is shared with the caller of the script. When called from the command line, they share the base workspace. When called from a function, they share that function's workspace.
• Creating Global VariablesEach function that uses a global variable must first declare the variable as global. You would declare global variable MAXLEN as follows: global MAXLEN
• To access the variable from the MATLAB command line, you must declare it as global at the command line.
Global VariablesThen in the command prompt enter the statements
global ALPHAALPHA = 0.01y=myfunction(1:10);
The global statement make the values assigned to ALPHA at the command prompt available inside the function defined by myfunction.m. They can be modified interactively and new solutions obtained without editing any files.
• You must declare persistent variables as persistent before you can use them in a function. It is usually best to put persistent declarations toward the beginning of the function. You would declare persistent variable X as follows:
Unlike the C language switch construct, the MATLAB switch does not "fall through." That is, if the first case statement is true, other case statements do not execute. Therefore, break statements are not used.
Flow ControlThe for loop executes a statement or group of statements a predetermined number of times.
for index = indexstart:increment:indexendstatements
enddefault increment is 1.You can specify any increment, including a negative one. x=[1,2,3,8,4];for i = 2:6 x(i) = 2*x(i-1);endYou can nest multiple for loops.x=[1,2,3,8,4;4 9 7 10 15];m=length(x(:,1));n=length(x(1,:));for i = 1:m for j = 1:n A(i,j) = 1/(i + j - 1); endend
Vectorizing LoopsMATLAB is designed for vector and matrix operations.You can often speed up your M-file code by using vectorizing algorithms that take advantage of this design. Vectorization means converting for and while loops to equivalent vector or matrix operations.Compute the sine of 1001 values ranging from 0 to 10.tici = 0;for t = 0:.001:10 i = i+1; y(i) = sin(t);endtoc; t=tocA vectorized version of the same code is:tict = 0:.001:10;y = sin(t);toc
•The most commonly used, high-level, file I/O functions in MATLAB are save and load.
save•Saves workspace variables on disk. •As an alternative to the save function, select Save Workspace As from the File menu in the MATLAB desktop, or use the Workspace browser.
• Use the functional form of load, such as load('filename'), when the file name is stored in a string, when an output argument is requested, or if filename contains spaces. To specify a command line option with this functional form, specify the option as a string argument, including the hyphen.
Read an ASCII delimited file into a matrix Graphical InterfaceAs an alternative to dlmread, use the Import Wizard. To activate the Import Wizard, select Import data from the File menu. SyntaxM = dlmread(filename,delimiter)M = dlmread(filename,delimiter,R,C)M = dlmread(filename,delimiter,range)
Description• M = dlmread(filename,delimiter) reads numeric data from the
ASCII delimited file filename, using the specified delimiter. • default delimiter - comma (,)• '\t‘ - tab• M = dlmread(filename,delimiter,R,C) reads numeric data from
the ASCII delimited file filename, using the specified delimiter.• The values R and C specify the row and column where the
upper-left corner of the data lies in the file. R and C are zero based so that R=0, C=0 specifies the first value in the file, which is the upper left corner.
• M = dlmread(filename,delimiter,range) reads the range specified by range = [R1 C1 R2 C2] where (R1,C1) is the upper-left corner of the data to be read and (R2,C2) is the lower-right corner.
Remarks• dlmread fills empty delimited fields with zero. Data
files having lines that end with a non-space delimiter, such as a semi-colon, produce a result that has an additional last column of zeros.