Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
17
Добавлен:
09.02.2015
Размер:
237.57 Кб
Скачать

matlab\lang- Конструкции языка программирования

Control flow.

<if> - Conditionally execute statements.

IF Conditionally execute statements.

The general form of the IF statement is

IF expression

statements

ELSEIF expression

statements

ELSE

statements

END

The statements are executed if the real part of the expression

has all non-zero elements. The ELSE and ELSEIF parts are optional.

Zero or more ELSEIF parts can be used as well as nested IF's.

The expression is usually of the form expr rop expr where

rop is ==, <, >, <=, >=, or ~=.

Example

if I == J

A(I,J) = 2;

elseif abs(I-J) == 1

A(I,J) = -1;

else

A(I,J) = 0;

end

See also relop, else, elseif, end, for, while, switch.

Reference page in Help browser

doc if

<else> - Execute statement if previous IF condition failed.

ELSE Used with IF.

ELSE is used with IF. The statements after the ELSE are executed

if all the preceding IF and ELSEIF expressions are false.

The general form of the IF statement is

If expression

statements

ELSEIF expression

statements

ELSE

statements

END

See also if, elseif, end.

Reference page in Help browser

doc else

<elseif> - Execute if previous IF failed and condition is true.

ELSEIF IF statement condition.

ELSEIF is used with IF. The statements after the ELSEIF are

executed if the expression is true and all the preceding IF and

ELSEIF expressions are false. An expression is considered true if

the real part has all non-zero elements.

ELSEIF does not need a matching END, while ELSE IF does.

The general form of the IF statement is

If expression

statements

ELSEIF expression

statements

ELSE

statements

END

See also if, else, end.

Reference page in Help browser

doc elseif

<end> - Terminate scope of control statements.

END Terminate scope of FOR, WHILE, SWITCH, TRY, and IF statements.

Without END's, FOR, WHILE, SWITCH, TRY, and IF wait for further input.

Each END is paired with the closest previous unpaired FOR, WHILE,

SWITCH, TRY or IF and serves to terminate its scope.

END also marks the termination of an M-file function, although in

most cases it is optional. END statements are required only in

M-files that employ one or more nested functions. Within such an

M-file, every function (including primary, nested, private, and

subfunctions) must be terminated with an END statement. You can

terminate any function type with END, but doing so is not required

unless the M-file contains a nested function.

END can also serve as the last index in an indexing expression. In

that context, END = SIZE(X,k) when used as part of the k-th index.

Examples of this use are, X(3:end) and X(1,1:2:end-1). When using END

to grow an array, as in X(end+1) = 5, make sure X exists first.

END(A,K,N) is called for indexing expressions involving the object A

when END is part of the K-th index out of N indices. For example,

the expression A(end-1,:) calls A's END method with END(A,1,2).

See also for, while, switch, try, if.

Overloaded methods:

categorical/end

timer/end

serial/end

tscollection/end

instrument/end

gf/end

DynamicSystem/end

daqdevice/end

daqchild/end

distributed/end

codistributed/end

fints/end

idmodel/end

idfrd/end

iddata/end

idnlfunVector/end

imaqdevice/end

imaqchild/end

icgroup/end

xregpointer/end

guidarray/end

designdev/end

coninputfactor/end

xregtable/end

sweepsetfilter/end

sweepset/end

opcroot/end

uss/end

umat/end

ufrd/end

ndlft/end

dataset/end

qrandset/end

Reference page in Help browser

doc end

<for> - Repeat statements a specific number of times.

FOR Repeat statements a specific number of times.

The general form of a FOR statement is:

FOR variable = expr, statement, ..., statement END

The columns of the expression are stored one at a time in

the variable and then the following statements, up to the

END, are executed. The expression is often of the form X:Y,

in which case its columns are simply scalars. Some examples

(assume N has already been assigned a value).

for R = 1:N

for C = 1:N

A(R,C) = 1/(R+C-1);

end

end

Step S with increments of -0.1

for S = 1.0: -0.1: 0.0, do_some_task(S), end

Set E to the unit N-vectors

for E = eye(N), do_some_task(E), end

Long loops are more memory efficient when the colon expression appears

in the FOR statement since the index vector is never created.

The BREAK statement can be used to terminate the loop prematurely.

See also parfor, if, while, switch, break, continue, end, colon.

Reference page in Help browser

doc for

<parfor> - Parallel FOR-loop.

PARFOR Parallel FOR-loop

The general form of a PARFOR statement is:

PARFOR loopvar = initval:endval

<statements>

END

MATLAB executes the loop body denoted by STATEMENTS for a vector of

iterations specified by INITVAL and ENDVAL. If you have Parallel

Computing Toolbox, the iterations of STATEMENTS can execute in parallel

on separate MATLAB workers on your multi-core computer or computer

cluster. PARFOR differs from a traditional FOR loop in the following ways:

Iterations must be monotonically increasing integer values

Order in which the loop iterations are executed is not guaranteed

Restrictions apply to the STATEMENTS in the loop body

PARFOR (loopvar = initval:endval, M); <statements>; END uses M to

specify the maximum number of MATLAB workers that will evaluate

STATEMENTS in the loop body. M must be a nonnegative integer. By

default, MATLAB uses as many workers as it finds available. When there

are no MATLAB workers available or M is zero, MATLAB will still execute

the loop body in an iteration independent order but not in parallel.

In order to execute the iterations in parallel you must open a pool of

MATLAB workers using MATLABPOOL. MATLABPOOL is available with

Parallel Computing Toolbox.

EXAMPLE

Break three large eigenvalue computations across three computers or

cores:

matlabpool(3)

parfor i = 1:3

c(:,i) = eig(rand(1000));

end

See also for, matlabpool

Reference page in Help browser

doc parfor

<while> - Repeat statements an indefinite number of times.

WHILE Repeat statements an indefinite number of times.

The general form of a WHILE statement is:

WHILE expression

statements

END

The statements are executed while the real part of the expression

has all non-zero elements. The expression is usually the result of

expr rop expr where rop is ==, <, >, <=, >=, or ~=.

The BREAK statement can be used to terminate the loop prematurely.

For example (assuming A already defined):

E = 0*A; F = E + eye(size(E)); N = 1;

while norm(E+F-E,1) > 0,

E = E + F;

F = A*F/N;

N = N + 1;

end

See also for, if, switch, break, continue, end.

Reference page in Help browser

doc while

<break> - Terminate execution of WHILE or FOR loop.

BREAK Terminate execution of WHILE or FOR loop.

BREAK terminates the execution of FOR and WHILE loops.

In nested loops, BREAK exits from the innermost loop only.

BREAK is not defined outside of a FOR or WHILE loop.

Use RETURN in this context instead.

See also for, while, return, continue.

Reference page in Help browser

doc break

<continue> - Pass control to the next iteration of a loop.

CONTINUE Pass control to the next iteration of FOR or WHILE loop.

CONTINUE passes control to the next iteration of FOR or WHILE loop

in which it appears, skipping any remaining statements in the body

of the FOR or WHILE loop.

In nested loops, CONTINUE passes control to the next iteration of

FOR or WHILE loop enclosing it.

See also for, while, break, return.

Reference page in Help browser

doc continue

<switch> - Switch among several cases based on expression.

SWITCH Switch among several cases based on expression.

The general form of the SWITCH statement is:

SWITCH switch_expr

CASE case_expr,

statement, ..., statement

CASE {case_expr1, case_expr2, case_expr3,...}

statement, ..., statement

...

OTHERWISE,

statement, ..., statement

END

The statements following the first CASE where the switch_expr matches

the case_expr are executed. When the case expression is a cell array

(as in the second case above), the case_expr matches if any of the

elements of the cell array match the switch expression. If none of

the case expressions match the switch expression then the OTHERWISE

case is executed (if it exists). Only one CASE is executed and

execution resumes with the statement after the END.

The switch_expr can be a scalar or a string. A scalar switch_expr

matches a case_expr if switch_expr==case_expr. A string

switch_expr matches a case_expr if strcmp(switch_expr,case_expr)

returns 1 (true).

Only the statements between the matching CASE and the next CASE,

OTHERWISE, or END are executed. Unlike C, the SWITCH statement

does not fall through (so BREAKs are unnecessary).

Example:

To execute a certain block of code based on what the string, METHOD,

is set to,

method = 'Bilinear';

switch lower(method)

case {'linear','bilinear'}

disp('Method is linear')

case 'cubic'

disp('Method is cubic')

case 'nearest'

disp('Method is nearest')

otherwise

disp('Unknown method.')

end

Method is linear

See also case, otherwise, if, while, for, end.

Reference page in Help browser

doc switch

<case> - SWITCH statement case.

CASE SWITCH statement case.

CASE is part of the SWITCH statement syntax, whose general form is:

SWITCH switch_expr

CASE case_expr,

statement, ..., statement

CASE {case_expr1, case_expr2, case_expr3,...}

statement, ..., statement

...

OTHERWISE,

statement, ..., statement

END

See also switch, if, else, elseif, while, end.

Reference page in Help browser

doc case

<otherwise> - Default SWITCH statement case.

OTHERWISE Default SWITCH statement case.

OTHERWISE is part of the SWITCH statement syntax, whose general

form is:

SWITCH switch_expr

CASE case_expr,

statement, ..., statement

CASE {case_expr1, case_expr2, case_expr3,...}

statement, ..., statement

...

OTHERWISE,

statement, ..., statement

END

The OTHERWISE part is executed only if none of the preceding

case expressions match the switch expression.

See also switch, case.

Reference page in Help browser

doc otherwise

<try> - Begin TRY block.

TRY Begin TRY block.

The general form of a TRY statement is:

TRY

statement, ..., statement,

CATCH ME

statement, ..., statement

END

Normally, only the statements between the TRY and CATCH are executed.

However, if an error occurs while executing any of the statements, the

error is captured into an object, ME, of class MException, and the

statements between the CATCH and END are executed. If an error occurs

within the CATCH statements, execution stops, unless caught by another

TRY...CATCH block. The ME argument is optional.

See also catch, MException, rethrow, eval, evalin, end.

Reference page in Help browser

doc try

<catch> - Begin CATCH block.

CATCH Begin CATCH block.

The general form of a TRY statement is:

TRY

statement, ..., statement,

CATCH ME

statement, ..., statement

END

Normally, only the statements between the TRY and CATCH are executed.

However, if an error occurs while executing any of the statements, the

error is captured into an object, ME, of class MException, and the

statements between the CATCH and END are executed. If an error occurs

within the CATCH statements, execution stops, unless caught by another

TRY...CATCH block. The ME argument is optional.

See also try, MException, MException/rethrow, end.

Reference page in Help browser

doc catch

<return> - Return to invoking function.

RETURN Return to invoking function.

RETURN causes a return to the invoking function or to the keyboard.

It also terminates the KEYBOARD mode.

Normally functions return when the end of the function is reached.

A RETURN statement can be used to force an early return.

Example

function d = det(A)

if isempty(A)

d = 1;

return

else

...

end

See also function, keyboard, break, continue.

Reference page in Help browser

doc return

<error> - Display message and abort function.

ERROR Display message and abort function.

ERROR('MSGID', 'ERRMSG', V1, V2, ...) displays a descriptive message

ERRMSG when the currently-running M-file program encounters an error

condition. Depending on how the program code responds to the error,

MATLAB then either enters a catch block to handle the error condition,

or exits the program.

MSGID is a unique message identifier string that MATLAB attaches to the

error message to better identify the source of the error (see MESSAGE

IDENTIFIERS, below).

ERRMSG is a character string that informs the user about the cause of

the error and can also suggest how to correct the faulty condition.

The ERRMSG string may include predefined escape sequences, such as

\n for newline, and conversion specifiers, such as %d for a decimal

number.

Inputs V1, V2, etc. represent values or substrings that are to

replace conversion specifiers used in the ERRMSG string. The format

is the same as that used with the SPRINTF function.

ERROR('ERRMSG', V1, V2, ...) reports an error without including a

message identifier in the error report.

ERROR('ERRMSG') is the same as the above syntax, except that the ERRMSG

string contains no conversion specifiers, no escape sequences, and no

substitution value (V1, V2, ...) arguments.

The ERROR function also determines where the error occurred, and

provides this information in the STACK property of the structure

returned by MException.last. This field contains a structure array

that has the same format as the output of the DBSTACK function. This

stack points to the line where the ERROR function was called.

ERROR(MSGSTRUCT) reports the error using the MSGID and MESSAGE stored

in the scalar structure MSGSTRUCT. This structure contains a MSGID and

MESSAGE field, and may also include a STACK field. If MSGSTRUCT does

contain a STACK field, then MATLAB sets the STACK field of the error

to the STACK field of MSGSTRUCT.

If MSGSTRUCT is an empty structure, no action is taken and ERROR

returns without exiting the M-file.

MESSAGE IDENTIFIERS

A message identifier is a string of the form

[component:]component:mnemonic

that enables MATLAB to identify with a specific error. The string

consists of one or more COMPONENT fields followed by a single

MNEMONIC field. All fields are separated by colons. Here is an

example identifier that has 2 components and 1 mnemonic.

'myToolbox:myFunction:fileNotFound'

The COMPONENT and MNEMONIC fields must begin with an

upper or lowercase letter which is then followed by alphanumeric

or underscore characters.

The COMPONENT field specifies a broad category under which

Соседние файлы в папке Библиотеки Matlab