Matlab: Procedures and Functions<br />
Script files<br />Script files, also called M- files as they have extension .m, make MATLAB programming much more efficien...
Script files<br />Script files, also called M- files as they have extension .m, make MATLAB programming much more efficien...
Script files<br />Here’s an example of what a script file looks like:<br />
Script files<br />Important commands:<br />for- To print &quot;Hello World&quot; 10 times write<br /><ul><li>for n=1:10
disp('Hello World')
 end</li></li></ul><li>Script files<br />Important commands:<br />if, else and else if-<br /><ul><li>if a == b
a = b + 1
elseif a > b
 a = b - 1
else a = b
 end</li></li></ul><li> User Defined Functions<br />When you use “sin” or “log” or “exp” in Matlab you are using “function...
 User Defined Functions<br />An example of a function:<br />
String Handling<br />Creating strings<br />Strings are matrices with character elements. The simplest way to create a stri...
String Handling<br />&gt;&gt; string=&apos;This is a string‘<br />&gt;&gt; string(3) % gives the third element in the vari...
String Handling<br />&gt;&gt; Y= input(&apos;Please type something here: &apos;,&apos;s&apos;); <br />% Matlab expects a  ...
Upcoming SlideShare
Loading in...5
×

Procedures And Functions in Matlab

2,255

Published on

Procedures And Functions in Matlab

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,255
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Procedures And Functions in Matlab

  1. 1. Matlab: Procedures and Functions<br />
  2. 2. Script files<br />Script files, also called M- files as they have extension .m, make MATLAB programming much more efficient than entering individual commands at the command prompt. A script file consists of MATLAB commands that together perform a specific task. The M-file is a text file which can be created and edited by any plain text editor like Notepad, emacs or the built-in MATLAB editor. <br />
  3. 3. Script files<br />Script files, also called M- files as they have extension .m, make MATLAB programming much more efficient than entering individual commands at the command prompt. A script file consists of MATLAB commands that together perform a specific task. The M-file is a text file which can be created and edited by any plain text editor like Notepad, emacs or the built-in MATLAB editor. <br />
  4. 4. Script files<br />Here’s an example of what a script file looks like:<br />
  5. 5. Script files<br />Important commands:<br />for- To print &quot;Hello World&quot; 10 times write<br /><ul><li>for n=1:10
  6. 6. disp('Hello World')
  7. 7. end</li></li></ul><li>Script files<br />Important commands:<br />if, else and else if-<br /><ul><li>if a == b
  8. 8. a = b + 1
  9. 9. elseif a > b
  10. 10. a = b - 1
  11. 11. else a = b
  12. 12. end</li></li></ul><li> User Defined Functions<br />When you use “sin” or “log” or “exp” in Matlab you are using “function m-files”. They are different from “script m-files” primarily because they have inputs and outputs. To specify which variables in the m-file are the inputs, and which are the outputs, the first line of the m-file should be in this form:<br /> function output=function_name(input)<br />
  13. 13. User Defined Functions<br />An example of a function:<br />
  14. 14. String Handling<br />Creating strings<br />Strings are matrices with character elements. The simplest way to create a string is to use it on the left side of an equal sign where the right side of the equal sign is an expression that evaluates to a string. String constants (literals) are enclosed in single quotes. The following example shows how to create string variables.<br />&gt;&gt; first = &apos;John&apos;;<br /> &gt;&gt; last = &apos;Coltrane&apos;; <br />&gt;&gt; name = [first,&apos; &apos;,last] <br />
  15. 15. String Handling<br />&gt;&gt; string=&apos;This is a string‘<br />&gt;&gt; string(3) % gives the third element in the variable words. <br />ans = i<br />&gt;&gt; string(2)=&apos;t&apos; % replaces the second element in the vector with t.<br />
  16. 16. String Handling<br />&gt;&gt; Y= input(&apos;Please type something here: &apos;,&apos;s&apos;); <br />% Matlab expects a number, string or a matrix from the keyboard. <br />% The second argument says the returned variable should be a string.<br />&gt;&gt; disp(&apos;The name is: &apos;), disp(x) <br />% gives text as output and the value of x.<br />
  17. 17. String Handling<br />Also, different formats can be converted to ‘strings’ and vice verse. Here are some examples of functions that help in converting between different formats:<br />int2str(n)  Converts an integer n to a string<br />hex2num(hstr)  Converts hexadecimal number hstr to a float.<br />hex2dec(hstr)  Converts hexadecimal string to decimal integer.<br />dec2hex(n)  Converts decimal integer to hexadecimal string. bin2dec(str)  Converts binary string to decimal integer. mat2str(A,n)  Convert a 2-D matrix to a string in MATLAB syntax.<br />
  18. 18. String Handling<br />There are several functions available to manipulate or fetch appropriate data from strings:<br />blanks(n)  Gives a string with n blanks<br />deblank(str)  Subtracts all blanks at the end of the string.<br />lower(str)  All letters are changed to small.<br />upper(str)  All letters are changed to capital.<br />ischar(str)  If string contains character =&gt; gives 1 in return, 0 otherwise.<br />
  19. 19. String Handling<br />There are several functions available to manipulate or fetch appropriate data from strings:<br />isletter(str(i))  If element number i in the string is a letter=&gt; gives one in return.<br />isspace(str)  True for white space characters.<br />strcmp(str1,str2)  returns 1 if strings S1 and S2 are the same and 0 otherwise.<br />strcmpi(str1,str2)  returns 1 if strings S1 and S2 are the same except forcase and 0 otherwise.<br />
  20. 20. String Handling<br />There are several functions available to manipulate or fetch appropriate data from strings:<br />strfind(str1,str2)  returns the starting indices of any occurrences of the string str2 in the string str1.<br />findstr(str1,str2)  returns the starting indices of any occurrences of the shorter of the two strings in the longer.<br />
  21. 21. Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at www.dataminingtools.net<br />

×