1 of 23

Use of MATLAB in food industry; computing with MATLAB, script files and editor/debugger, MATLAB help system, problem solving methodologies, numeric, cell, arrays, matrix operations, user defined functions, programming using MATLAB; debugging MATLAB programs, applications to simulations

COM 103 ICT Applications in Food Industry (1+2)

Lecture Schedule 5 – Part - II

Dr. S. Sridevy, Ph.D.

Assistant Professor (Computer Science)

Dept. of Physical Sciences & IT

AEC&RI, TNAU, Coimbatore – 3.

3 June 2020

2 of 23

Naming Rules for Variables

  • Variable names must begin with a letter
  • Names can include any combinations of letters, numbers, and underscores
  • Maximum length for a variable name is 63 characters
  • MATLAB® is case sensitive. The variable name A is different than the variable name a.
  • It is good programming practice to name your variables to reflect their function in a program rather than using generic x, y, z variables.

S. Sridevy

3 June 2020

3 of 23

Creating Strings (Text Variables)

>> month = ‘August’

  • A variable, month, of type string (character array) is created
  • A memory location for the variable month is assigned
  • The string August is stored in that memory location called month.

S. Sridevy

3 June 2020

4 of 23

Displaying Variables

  • To display the variable type the name of the variable in the command prompt.
  • Or a function called disp to display variables.
  • Type the following commands at the command prompt:

>> disp(‘The value of x is:’); disp(x)

S. Sridevy

3 June 2020

5 of 23

Script Files

  • All of the pre-built commands in MATLAB® are script files or functions (plot, mean, std, exp, cosd, …)
  • MATLAB® allows the user to create customized m-files for specific applications or problems.

S. Sridevy

3 June 2020

6 of 23

Script Files

  • A script file is simply a collection of executable MATLAB® commands. To create a new script file, click on the New Script icon on the left side of the Home Tab.

S. Sridevy

3 June 2020

7 of 23

Script Files

Click on New Script

Creates Blank Script File

8 of 23

Script File Procedure

  • Type a set of executable commands in the editor window.
  • Save the file in an appropriate folder.
  • Set the current directory in MATLAB® to the same place where you saved the script file.
  • To run the script file: Hit the Green Run Arrow in the Editor window or simply type the name of the file (without the .m extension) at the command prompt in the MATLAB command window.
  • The file must be in your current directory or it will not run!

S. Sridevy

3 June 2020

9 of 23

Exercise 1

  • Right click in the current folder window in MATLAB and create a new folder named whatever you would like.
  • Double click on the folder to make it your current folder.
  • Clear your MATLAB workspace by typing clear at the command prompt.
  • Click on New Script to open a blank script file.
  • Type the commands on the next slide into the editor/debugger window then save the file as CircleScript in your newly created folder.

S. Sridevy

3 June 2020

10 of 23

Exercise 1: Script File

Save the file as CircleScript in newly created folder.

Note: Any line that starts with a % is a comment and

turns green – it doesn’t execute.

11 of 23

Script Files

  • A script file is simply a set of executable MATLAB commands saved together in a file.
  • It behaves exactly the same as entering each command sequentially in the command window.
  • Every single variable defined appears in the workspace.

S. Sridevy

3 June 2020

12 of 23

Script Files

  • Script files can be really useful if you are going to execute a lot of MATLAB commands.
  • For example, suppose you execute 15 commands at the command prompt and discover an error in the first command that affected the last 14 commands.
  • In the command window, you would have to fix the error in the first command then run the other 14 commands over again.
  • If these commands were in a script file, you could fix the first command and re-run the script file – much faster !!

S. Sridevy

3 June 2020

13 of 23

Default MATLAB Desktop

S. Sridevy

3 June 2020

14 of 23

Command Window

  • Command window to communicate with the MATLAB program, by typing instructions of various types called commands, functions, and statements.
  • MATLAB displays the prompt (>>) to indicate that it is ready to receive instructions.

S. Sridevy

3 June 2020

15 of 23

Current Directory Window

  • The Current Directory window is much like a file manager window used to access files.
  • Double-clicking on a file name with the extension .m will open that file in the MATLAB Editor.

S. Sridevy

3 June 2020

16 of 23

Workspace Window

  • The term workspace refers to the names and values of any variables in use in the current work session.
  • The Workspace window appears in the upper right.
  • The Workspace window displays the variables created in the Command window.
  • Double-clicking on a variable name opens the Array Editor

S. Sridevy

3 June 2020

17 of 23

Command History Window

  • Shows all the previous keystrokes entered in the Command window.
  • Useful for keeping track of what is typed.
  • Clicking on a keystroke and drag it to the Command window or the Editor to avoid retyping it.
  • Double-clicking on a keystroke executes it in the Command window.

S. Sridevy

3 June 2020

18 of 23

MATLAB Help System

  • With four options:

1. Function Browser

    • provides quick access to the documentation for the MATLAB function.

2. Help Browser

    • This graphical user interface gives information and view online documentation of MathWorks products.

3. Help Functions

    • The functions help, lookfor, and doc can be used to display syntax information for a speci ed function.

4. Other Resources

    • For additional help, you can run demos, contact technical support, search documentation for other MathWorks products, view a list of other books, and participate in a newsgroup.

S. Sridevy

3 June 2020

19 of 23

Problem Solving Methodologies

1.Understand the purpose of the problem.

2. Collect the known information. Realize that some of it might later be found unnecessary.

3. Determine what information you must find.

4. Simplify the problem only enough to obtain the required information. State any assumptions you make.

S. Sridevy

3 June 2020

20 of 23

Problem Solving Methodologies (Contd.)

5. Draw a sketch and label any necessary variables.

6. Determine which fundamental principles are applicable.

7. Think generally about your proposed solution approach and consider other approaches before proceeding with the details.

S. Sridevy

3 June 2020

21 of 23

Problem Solving Methodologies (Contd.)

8. Label each step in the solution process.

9. If you solve the problem with a program, hand check the results using a simple version of the problem. Checking the dimensions and units and printing the results of intermediate steps in the calculation sequence can uncover mistakes.

S. Sridevy

3 June 2020

22 of 23

Problem Solving Methodologies (Contd.)

10. Perform a “reality check” on your answer. Does it make sense? Estimate the range of the expected result and compare it with your answer. Do not state the answer with greater precision than is justified by any of the following:

    • The precision of the given information.
    • (b) The simplifying assumptions.
    • (c) The requirements of the problem.

S. Sridevy

3 June 2020

23 of 23

Problem Solving Methodologies (Contd.)

  • Interpret the mathematics. If the mathematics produces multiple answers, do not discard some of them without considering what they mean. The mathematics might be trying to tell you something, and you might miss an opportunity to discover more about the problem.

S. Sridevy

3 June 2020

Thank You!