• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Matlab: Procedures And Functions
 

Matlab: Procedures And Functions

on

  • 7,136 views

Matlab: Procedures And Functions

Matlab: Procedures And Functions

Statistics

Views

Total Views
7,136
Views on SlideShare
7,108
Embed Views
28

Actions

Likes
1
Downloads
0
Comments
0

3 Embeds 28

http://www.dataminingtools.net 21
http://dataminingtools.net 4
http://www.slideshare.net 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Matlab: Procedures And Functions Matlab: Procedures And Functions Presentation Transcript

    • Matlab: Procedures and Functions
    • Script files
      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. 
    • Script files
      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. 
    • Script files
      Here’s an example of what a script file looks like:
    • Script files
      Important commands:
      for- To print "Hello World" 10 times write
      • for n=1:10
      • disp('Hello World')
      • end
    • Script files
      Important commands:
      if, else and else if-
      • if a == b
      • a = b + 1
      • elseif a > b
      • a = b - 1
      • else a = b
      • end
    • User Defined Functions
      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:
      function output=function_name(input)
    • User Defined Functions
      An example of a function:
    • String Handling
      Creating strings
      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.
      >> first = 'John';
      >> last = 'Coltrane';
      >> name = [first,' ',last]
    • String Handling
      >> string='This is a string‘
      >> string(3) % gives the third element in the variable words.
      ans = i
      >> string(2)='t' % replaces the second element in the vector with t.
    • String Handling
      >> Y= input('Please type something here: ','s');
      % Matlab expects a number, string or a matrix from the keyboard.
      % The second argument says the returned variable should be a string.
      >> disp('The name is: '), disp(x)
      % gives text as output and the value of x.
    • String Handling
      Also, different formats can be converted to ‘strings’ and vice verse. Here are some examples of functions that help in converting between different formats:
      int2str(n)  Converts an integer n to a string
      hex2num(hstr)  Converts hexadecimal number hstr to a float.
      hex2dec(hstr)  Converts hexadecimal string to decimal integer.
      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.
    • String Handling
      There are several functions available to manipulate or fetch appropriate data from strings:
      blanks(n)  Gives a string with n blanks
      deblank(str)  Subtracts all blanks at the end of the string.
      lower(str)  All letters are changed to small.
      upper(str)  All letters are changed to capital.
      ischar(str)  If string contains character => gives 1 in return, 0 otherwise.
    • String Handling
      There are several functions available to manipulate or fetch appropriate data from strings:
      isletter(str(i))  If element number i in the string is a letter=> gives one in return.
      isspace(str)  True for white space characters.
      strcmp(str1,str2)  returns 1 if strings S1 and S2 are the same and 0 otherwise.
      strcmpi(str1,str2)  returns 1 if strings S1 and S2 are the same except forcase and 0 otherwise.
    • String Handling
      There are several functions available to manipulate or fetch appropriate data from strings:
      strfind(str1,str2)  returns the starting indices of any occurrences of the string str2 in the string str1.
      findstr(str1,str2)  returns the starting indices of any occurrences of the shorter of the two strings in the longer.
    • Visit more self help tutorials
      Pick a tutorial of your choice and browse through it at your own pace.
      The tutorials section is free, self-guiding and will not involve any additional support.
      Visit us at www.dataminingtools.net