机械与机电工程学系(1).pdf
1 沈志忠 機械與機電工程學系 Programming in MatlabProgramming in Matlab Chapter 1 – Introduction to Matlab 2 Programming in MatlabProgramming in Matlab 1.3 The Matlab Environment 2 3 Programming in MatlabProgramming in Matlab The MATLAB Desktop The Command Window The Command History Window Launch Pad The Edit/Debug Window Figure Windows Workspace Browser and Array Editor Help Browser Current Directory Browser 4 Programming in MatlabProgramming in Matlab 3 5 Programming in MatlabProgramming in Matlab The Command Window MATLAB calculates the answer as soon as the Enter key is presses and store the answer in a variable. area pi * 2.52 area 19.6350 If a statement is too long, it may be contnued on seccessive lines by typeing an ellipsis ... x1 1 1/2 1/3 1/4 1/5 1/6; x1 1 1/2 1/3 1/4 ... 1/5 1/6; A series of commands may be placed into a file, called script file or M-file with a file extension ”.m”. 6 Programming in MatlabProgramming in Matlab The Command History Window 4 7 Programming in MatlabProgramming in Matlab The Workspace Browser 8 Programming in MatlabProgramming in Matlab The Help Browser 5 9 Programming in MatlabProgramming in Matlab The Help System 1. Select the Help icon from the desktop toolbar. 2. Search for appropriate function lookfor keyword 3. Rapid help with syntax and function definition help function An advanced hyperlinked help system is launched by helpdesk Complete manuals as PDF files 10 Programming in MatlabProgramming in Matlab A Few Important Commands demo to run MATLAB’s built-in demonstrations clc to clear the contents of the Command Window clg to clear the contents of the current Figure Window clear to clear the contents of the Workspace C control-c to abort the cution of a program diary fileman to keep track of everything done during a MATLAB session diary on diary off 6 11 Programming in MatlabProgramming in Matlab The MATLAB Search Path MATLAB can’t tell if identifier is variable or function MATLAB searches for identifier in the following order 1. variable in current workspace 2. built-in variable 3. built-in m-file 4. m-file in current directory 5. m-file on search path Note m-files can be located in current directory, or in path 12 Programming in MatlabProgramming in Matlab The MATLAB Search Path Programming Pitfalls Never use a variable with the same name as a MATLAB function or command. That function or command will become inaccessible. Never create an M-file with the the same name as a MATLAB function or command. 7 13 Programming in MatlabProgramming in Matlab The MATLAB Search Path which function to find out which version of a file and where it is located The path variable defines where matlab searches for m-files path display MATLAB search path addpath add directrory to MATLAB search path pathtool display MATLAB search path 14 Programming in MatlabProgramming in Matlab Next Lecture MATLAB Basics