📄 tutor2.m
字号:
% ADSP Toolbox: Version 2.0
% For use with "Analog and Digital Signal Processing", 2nd Ed.
% Published by PWS Publishing Co.
%
% Ashok Ambardar, EE Dept. MTU, Houghton, MI 49931, USA
% http://www.ee.mtu/faculty/akambard.html
% e-mail: akambard@mtu.edu
% Copyright (c) 1998
clc,echo on
% EXPRESSIONS AND STRING FUNCTIONS
%MATLAB has the capability of evaluating expressions if they are first defined
%as string functions.
%To define y=4*x+3 as a string expression we use single quotes:
y='4*x+3'
%Note how MATLAB answers with the actual expression 4x+3 .
%Note that, y='4x+3' cannot be evaluated until x is assigned some value(s).
pause %Strike a key to continue
%With y='4x+3', here is how we evaluate x at x=0, 2, 3 and 5. We type
x=[0 2 3 5] %Values of x in an array
z=eval(y) %z yields y evaluated for various x
%Values of z can now be used for any purpose.
pause %Strike a key to continue
clc
% FUNCTION FILES
%MATLAB allows us to use our own functions by defining them in an m-file.
%Such an m-file is called a FUNCTION M-FILE.
%As an example sinc(x) is defined as sin(pi*x)/(pi*x) in the m-file sinc.m
%To see its contents, we issue the command type sinc (without the extension).
pause %Strike a key to see the contents of sinc.m
echo off
disp('function y = sinc(x)')
disp('% SINC Sinc Function.')
disp('%')
disp('% Y=SINC(X) generates sin(pi*x)/(pi*x)')
disp(' ')
disp('x=pi*x;i=find(x==0);[m,n]=size(i);x(i)=ones(m,n);')
disp('y=sin(x)./x;y(i)=ones(m,n);')
echo on
%EXPLANATION: The first line is the function definition and arguments
%Lines after % are comments that appear when we type HELP SINC
%NOTES: The actual function evaluation follows the help section
%The internal variables of the m-file are not stored in the workplace
%Only the final resut is assigned to the output!!
%Even though the output and input arguments are y and x, we can assign
%any variables of our choice to evaluate the function.
pause %Strike a key to continue
%For example, with t=-2:0.5:1; we can evaluate z=sinc(t+1) by typing
t=-2:0.5:1
z=sinc(t+2)
%Function files allow us to extend MATLAB. Many of the m-files that come
%supplied with the text are function files to perform various tasks.
pause %Strike a key to continue
clc
% FUNCTION EVALUATION
%Functions in a function m-file can be evaluated using the command feval
%This facility is often used within other function m-files.
%Thus if sinc.m is a function m-file for computing sinc(x),
%we can evaluate it for f=0:.25:1 by typing
f=0:.25:1
p=feval('sinc',f)
%Notice how sinc is entered as a string function.
%It is followed by all the needed input arguments.
pause %Strike a key to continue
clc
% SCRIPT M-FILES
%Script m-files are simply a series of MATLAB commands stored in an m-file
%They are different from function m-files.
%Typing the filename simply executes the commands in succession.
%All the variables remain in the workplace.
%An example of a script m-file is converr
pause %Strike a key to see its contents
type converr
pause %Strike a key to continue
%Script files may contain any commands and call any functions.
%To see what converr does (while suppressing the output), we simply type
%converr
pause %Strike a key to continue
echo off,converr;echo on
%Function files and script files are ASCII files and may be created using
%any text editor or word processor capable of storing ASCII text.
pause %Strike a key to continue
clc
% DIARY
%The commands executed in a MATLAB session can be stored in a file
%To store subsequent commands in the file junk.m, we enter the command
diary junk.m
x=0:.2:1
%All MATLAB commands and text screen displays are stored until
%we issue the command
diary off
pause %Strike a key to continue
%Here are what is stored in the diary
type junk.m
pause %Strike a key to continue
%This file may be used as a script file to run the commands but it must be
%first cleaned up using a text editor by retaining only the commands
%and deleting all prompt signs and screen output.
%To append more commands, enter diary on
%To create a new diary say junk2.m, enter diary junk2.m
%NOTE: MAKE SURE YOU DELETE THIS FILE BY TYPING THE COMMAND APPROPRIATE
%FOR YOUR OPERATING SYSTEM (from within MATLAB or otherwise)
pause %Strike a key to END THIS PORTION
clc,echo off
clear
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -