MATLAB Programming Language by K. Parker

MATLAB Programming Language by K. Parker

Author:K. Parker
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2016-10-14T23:00:00+00:00


Examples of Functions that Return a Logical Result

MATLAB functions that test the state of a variable or expression return alogicalresult:

A = isstrprop('abc123def', 'alpha') A=

111 00011 1

Logical functions such as xorreturn a logical result:

xor([1 0 'ab' 2.4], [ 0 0 'ab', 0]) ans =

Note however that the bitwise operators do not return a logical:

X = bitxor(3, 12);

whos X

Name Size Bytes Class Attributes

X 1x1 8 double

String comparison functions also return a logical: S = 'D:\matlab\mfiles\test19.m'; strncmp(S, 'D:\matlab', 9) ans =

1 Note the difference between the elementwise and short-circuit logical operators. Short-circuit operators, such as &&and ||, test only as much of the input expression as necessary. In the second part of this example, it makes no difference that Bis undefined because the state of Aalone determines that the expression is false:

A= 0;

A&B

Undefined function or variable 'B'.

A&&B ans =

0

One way of implementing an infinite loop is to use the whilefunction along with the logical constant true: while true

a = []; b = [];

a = input('Enter username: ', 's');

if ~isempty(a)

b = input('Enter password: ', 's'); end

if ~isempty(b) disp 'Attempting to log in to account ...' break

end

end



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.