Matlab for Signal Processing- Lesson 03
- Grigoris Athanasiadis
- 23 Οκτ 2017
- διαβάστηκε 2 λεπτά

The main constructs for iteration (looping) in MATLAB are the for and while statements. Note the use of the step increment of 0.1 in the following.
for x = 0:0.1 :10
fprintf(1, 'x is %d\n', x);
end
The variable x takes on values 0, 0.1, 0.2, . . . 10 on each iteration. It is typically used where the terminating condition for the iteration is calculated within the loop, rather than iterating over a fixed range of values as with the for loop. Care should be exercised when testing for exact equality, and where very large or very small numbers might be encountered. MATLAB has the built - in variables realmin and realmax for the smallest and largest positive numbers, and eps for fl oating - point relative accuracy. While the largest number MATLAB can represent is quite large, and the smallest number is quite small, certain situations may cause problems in signal processing code, particularly where iterative calculations are involved. For example, the following loop should never terminate, because it calculates the sequence 1, 0.5, 0.25, . . . .
Functions
MATLAB code is placed in “ m - files, ” which are just plain text files with extension (suffix) .m. In order to be able to find the place (folder/ directory) where the m - files have been placed, they must be in the current MATLAB directory or in the MATLAB search path.
Type pwd to see the current directory, and cd to change to another directory — for example, typing
cd c:\matlab\work
will change the current working directory to disk drive c: in directory \matlab\work . Type path to see the current search path.
The commands clear all and close all are often used when beginning a MATLAB session. They are used to clear all variables and close all figure windows, respectively. MATLAB will use a fi le called startup.m in the current directory when it starts up, to set some initial conditions. This file is optional, but can be useful in larger projects. A simple startup.m fi le is shown below:
%add extra paths
addpath('C:\matlab\lib');
%start in this directory
cd C:\matlab\work
A function example






Σχόλια