CS1371_11_Scripts_and_Functions

CS1371_11_Scripts_and_Functions - CS1371 Introduction to...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS1371 Introduction to Computing for Engineers Scripts and Functions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Matlab Scripts and Functions Objectives Topics Matlab scripts Basics of a Matlab function Workspaces in Matlab Using functions in program design (functional programming ) Scripts How to design a function Function workspace Encapsulation Using functions Examples
Background image of page 2
3 Scripts and Functions Matlab’s Command prompt is where you can enter commands to be executed immediately… Just like a calculator You can see what you’ve done but it must be re-entered at the command prompt to be recalculated Only the results (variables) are retained in the Matlab workspace ( diary on will log commands and results to a diary file; diary off disables this) What if you want to enter different values for radius or length? >> length=5.5; >> radius=2.25; >> volume=pi.*radius^2.*length volume = 87.4737
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Matlab Scripts Matlab scripts are the solution to this problem! You can create a “script” that can be repeatedly executed This is the basic Matlab “program” Scripts are simply text files containing Matlab statements You can use any text editor but the built-in editor indents and uses color to highlight the language syntax Script files always have the “.m” extension, e.g., m-files When a script (m-file) is executed, it is simply read sequentially and each line is presented to the Matlab command prompt just like it was typed by hand Speed and repeatability are key features Matlab control & loop statements (e.g., if, for, while…) can be executed in this way
Background image of page 4
5 Example Script in an m-file Use File/New/M-file to start Matlab editor Save file with .m extension in directory in Matlab’s path Type m-file name at prompt to execute >> myscript Enter the length of a cylinder: 8 Now enter the radius: 2.4 Volume of cylinder is: 144.76 >> myscript Enter the length of a cylinder: 10 Now enter the radius: 1 Volume of cylinder is: 31.416 >>
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Script Workspace When commands are executed, the results are left in the Matlab BASE workspace . whos will list all the current variables in the workspace Scripts can make use of variables already defined in the Base workspace >> myscript Enter the length of a cylinder: 10 Now enter the radius: 1 Volume of cylinder is: 31.416 >> whos Name Size Bytes Class length 1x1 8 double array radius 1x1 8 double array volume 1x1 8 double array Grand total is 3 elements using 24 bytes >> >> density=100; >> myscript Enter the length of a cylinder: 10 Now enter the radius: 1 Weight of cylinder is: 3141.6 >>
Background image of page 6
7 More Scripts… First, try out statements individually in Command window… Use scripts to collect together statements that can be used to solve more complicated problems. Scripts can call other scripts
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/23/2012 for the course CS 1371 taught by Professor Stallworth during the Fall '08 term at Georgia Institute of Technology.

Page1 / 32

CS1371_11_Scripts_and_Functions - CS1371 Introduction to...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online