MATLAB程序设计基础

MATLAB程序设计基础
山东大学控制科学与工程学院
李晓磊
MATLAB的发展历程
 MATLAB - MATrix LABoratory
 20世纪七十年代后期:时任美国新墨西哥大学计
算机科学系主任的Cleve Moler教授用FORTRAN
设计了一组程序接口,此即萌芽状态的MATLAB。
 1984年成立了MathWorks公司,并把MATLAB正
式推向市场。从这时起,MATLAB的内核采用C语
言编写,而且除原有的数值计算能力外,还新增了
数据图视功能。
 进入20世纪九十年代的时候,MATLAB已经成为
国际控制界公认的标准计算软件。
主要版本
 1993年推出MATLAB4.0版本 (1)推出了SIMULINK。这是
一个交互式操作的动态系统建模、仿真、分析集成环境。(2)
开发了与外部进行直接数据交换的组件,打通了MATLAB进行
实时数据分析、处理和硬件开发的道路。(3)推出了符号计
算工具包。(4)Notebook 实现了MATLAB与Word的无缝连
接。其升级版本MATLAB4.2c应用较广泛。
 1997年,MATLAB5.0版问世,紧接着是5.1、5.2,以及和
1999年春的5.3版,真正32位运算,功能强大,数值计算加快,
拥有更丰富的数据类型和结构、更友善的面向对象、更加快速
精良的图形可视、更广博的数学和数据分析资源、更多的应用
开发工具。其中MATLAB5.3应用最广泛。
 2001年推出了MATLAB6.0,直到现在广泛使用的MATLAB
6.5,其界面有较大的改观,计算速度有了比较大的改善,增
加了与Java的接口 。
MATLAB应用与网上资源
 工具箱
信号处理、神经网络、鲁棒控制、系统辨识、
控制系统、图形处理、模糊逻辑、小波分析、
金融管理、概率统计、非线性控制等。
 网络资源
www.mathworks.com
ftp.mathworks.com
MATLAB安装
 硬件要求
x86:
 软件要求
Windows:
Linux:
Adobe acrobat reader
MATLAB程序执行方式
 立即执行
» 3.5*6.2
» exp(j*3*pi/4)
ans =
ans =
21.7000
»
-0.7071 + 0.7071i
»
 M文件:纯文本文件,扩展名为M,用任何
字处理软件都可以编辑修改。
?type exa1.m
%This is an example.
exp(j*3*pi/4)
?exa1
ans =
-0.7071 + 0.7071i
MATLAB命令窗口
 计算机安装好MATLAB之后,双击MATLAB
图标,就可以进入命令窗口,此时意味着系
统处于准备接受命令的状态,可以在命令窗
口中直接输入命令语句。
 通用命令
cd 显示或改变工作目录
hold 图形保持开关
dir 显示目录下的文件
type 显示文件内容
clear 清理内存变量
clf 清除图形窗口
pack 收集内存碎片
clc 清除工作窗
echo 工作窗信息显示开关
disp 显示变量或文字内容
path 显示搜索目录
save 保存内存变量到指定文件
load 加载指定文件的变量
diary 日志文件
quit 退出MATLAB
! 调用dos命令
 命令行键盘技巧

调用上一行

调用下一行
 光标左移一个字符
 光标右移一个字符
Ctrl+  光标左移一个单词
Ctrl+  光标右移一个单词
home 光标置于当前行开头
end 光标置于当前行末尾
esc 清除当前输入行
del 删除光标处的字符
backspace 删除光标前的字符
alt+backspace 恢复上一次的删除
 标点
: 具有多种应用功能
. 小数点及域访问符
;区分行,取消运行显示等
,区分列,函数参数分隔符
() 指定运算先后次序
[] 矩阵定义标志
{} 用于构成单元数组
… 续行符
% 注释标记
! 调用操作系统运算
= 赋值标记
‘ 字符串标示符
MATLAB搜索路径
 MATLAB的一切操作都是在其搜索路径中进
行的,如果调用的函数在搜索路径之外,系
统将认为该函数不存在。
 路径设置方法
 1、通过对话框设置
在MATLAB主窗口菜单中【file】-
【set Path】选项。
 2、path命令
2.1 path命令
?path
MATLABPATH
E:\MATLAB\works
E:\MATLAB\toolbox\matlab\general
E:\MATLAB\toolbox\matlab\ops
E:\MATLAB\toolbox\matlab\lang
E:\MATLAB\toolbox\matlab\elmat
E:\MATLAB\toolbox\matlab\elfun
E:\MATLAB\toolbox\matlab\specfun
E:\MATLAB\toolbox\matlab\matfun
E:\MATLAB\toolbox\matlab\datafun
E:\MATLAB\toolbox\matlab\polyfun
……
2.2 genpath命令
?genpath
ans =
E:\MATLAB\toolbox\matlab\datafun;E:\MATLAB\toolbox\matlab\d
atatypes;E:\MATLAB\toolbox\matlab\demos;E:\MATLAB\toolbox\
matlab\elfun;E:\MATLAB\toolbox\matlab\elmat;E:\MATLAB\toolbo
x\matlab\funfun;E:\MATLAB\toolbox\matlab\general;E:\MATLAB\t
oolbox\matlab\graph2d;E:\MATLAB\toolbox\matlab\graph3d;E:\M
ATLAB\toolbox\matlab\graphics;E:\MATLAB\toolbox\matlab\iofun;
E:\MATLAB\toolbox\matlab\iofun\src;E:\MATLAB\toolbox\matlab\l
ang;E:\MATLAB\toolbox\matlab\matfun;
2.3 editpath或pathtool命令
2.4 扩展搜索路径
将新目录扩展到搜索路径中
1、用path命令
?path(path,'e:\matlab\works')
2、用addpath命令
将新目录加到搜索路径的末尾
?addpath e:\matlab\works –end
将新目录加到搜索路径的开始
?addpath e:\matlab\works -begin
3、使用路径设置对话框
MATLAB帮助
 MATLAB Help
完善的HTML格式联机帮助系统,非常全面。
使用方法:
1.从help菜单中选取;
2.在命令窗口中执行helpdesk或doc。
 PDF文档
用Adobe acrobat reader阅读。
Help系列
 helpwin
 Help
?help
HELP topics:
matlab\general
matlab\ops
matlab\lang
matlab\elmat
matlab\elfun
matlab\specfun
matlab\matfun
matlab\datafun
……
For more help on
- General purpose commands.
- Operators and special characters.
- Programming language constructs.
- Elementary matrices and matrix manipulation.
- Elementary math functions.
- Specialized math functions.
- Matrix functions - numerical linear algebra.
- Data analysis and Fourier transforms.
directory/topic, type "help topic".
 Help + 函数(类)名
?help general
General purpose commands.
MATLAB Toolbox Version 5.2 18-Dec-1997
General information
help
- On-line help, display text at command line.
helpwin
- On-line help, separate window for navigation.
helpdesk - Comprehensive hypertext documentation and troubleshooti
demo
- Run demonstrations.
ver
- MATLAB, SIMULINK, and toolbox version information.
……
See also PUNCT.
?help helpwin
HELPWIN On-line help, separate window for navigation.
HELPWIN TOPIC opens a help window and displays the help text for the
given TOPIC. Links are created to functions referenced in the 'See Also'
line of the help text.
HELPWIN(HELP_STR,TITLE) displays the string HELP_STR in the help
window. HELP_STR may be passed in as a string with each line separated
by carriage returns, a column vector cell array of strings with each cell
(row) representing a line or as a string matrix with each row representing
a line. The optional string TITLE will appear in the title edit box.
HELPWIN({TITLE1 HELP_STR1;TITLE2 HELP_STR2;...},PAGE) displays one page
of multi-page help text. The multi-page help text is passed in as a
cell array of strings or cells containing TITLE and HELP_STR pairs.
Each row of the multi-page help text cell array (dimensioned number of
pages by 2) consists of a title string paired with a string, cell array
or string matrix of help text. The second argument PAGE is a string
which must match one of the TITLE entries in the multi-page help text.
The matching TITLE represents the page that is to be displayed first.
If no second argument is given, the first page is displayed.
A third argument may be passed to HELPWIN which is a string that becomes
the title of the help window figure.
Additional arguments, after the window title, will be interpreted as
Handle Graphics parameter-value pairs to be applied to the text displayed
in the help window.
Examples.
helpwin plot
helpwin('Help String','title')
helpwin(['Help text for' sprintf('\n') 'my m-file.'],'title')
helpwin({'Help String for';'my m-file'},'title')
helpwin(str,'Topic 2','My Title')
where,
str = { 'Topic 1' 'Help string for Topic 1';
'Topic 2' 'Help string for Topic 2';
'Topic 3' 'Help string for Topic 3' }
See also DOC, DOCOPT, HELP, WEB.
 Lookfor
根据用户提供的关键字搜索相关的函数。
?lookfor diff
SETDIFF Set difference.
DIFF Difference and approximate derivative.
POLYDER Differentiate polynomial.
ODE113 Solve non-stiff differential equations, variable order
method.
ODE15S Solve stiff differential equations, variable order method.
ODE23 Solve non-stiff differential equations, low order method.
ODE23S Solve stiff differential equations, low order method.
……
 帮助机理
 Help命令显示相应M文件的注释区
 Lookfor命令搜索路径中每个M文件的
第一行,扫描是否包含所要查询的字
符串。
?help exa1
This is an example.
 其他帮助命令




Exist 变量检验函数
What 目录中文件列表
Who 内存变量列表
Whos 内存变量详细信息
 Which 确定文件位置
联机演示系统
 基本介绍
Intro
 演示
demo