MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

2 Data Types A = 'fate'; B = 'cake'; A == B ans = 0 1 0 1 All of the relational operators (>, >=,

Characters and Strings A = isletter(mystring) A = 1 1 1 1 0 0 0 0 The first four elements in A are logical 1 (true) because the first four characters of mystring are letters. Searching and Replacing MATLAB provides several functions for searching and replacing characters in a string. (MATLAB also supports search and replace operations using regular expressions. See “Regular Expressions” on page 3-31.) Consider a string named label: label = 'Sample 1, 10/28/95'; The strrep function performs the standard search-and-replace operation. Use strrep to change the date from '10/28' to '10/30': newlabel = strrep(label, '28', '30') newlabel = Sample 1, 10/30/95 findstr returns the starting position of a substring within a longer string. To find all occurrences of the string 'amp' inside label, use position = findstr('amp', label) position = 2 The position within label where the only occurrence of 'amp' begins is the second character. The strtok function returns the characters before the first occurrence of a delimiting character in an input string. The default delimiting characters are the set of white-space characters. You can use the strtok function to parse a sentence into words. For example, function allWords = words(inputString) remainder = inputString; allWords = ''; 2-59

Characters and Strings<br />

A = isletter(mystring)<br />

A =<br />

1 1 1 1 0 0 0 0<br />

The first four elements in A are logical 1 (true) because the first four<br />

characters of mystring are letters.<br />

Searching and Replacing<br />

<strong>MATLAB</strong> provides several functions for searching and replacing characters in<br />

a string. (<strong>MATLAB</strong> also supports search and replace operations using regular<br />

expressions. See “Regular Expressions” on page 3-31.)<br />

Consider a string named label:<br />

label = 'Sample 1, 10/28/95';<br />

The strrep function performs the standard search-and-replace operation.<br />

Use strrep to change the date from '10/28' to '10/30':<br />

newlabel = strrep(label, '28', '30')<br />

newlabel =<br />

Sample 1, 10/30/95<br />

findstr returns the starting position of a substring within a longer string. To<br />

find all occurrences of the string 'amp' inside label, use<br />

position = findstr('amp', label)<br />

position =<br />

2<br />

The position within label where the only occurrence of 'amp' begins is the<br />

second character.<br />

The strtok function returns the characters before the first occurrence of a<br />

delimiting character in an input string. The default delimiting characters are<br />

the set of white-space characters. You can use the strtok function to parse a<br />

sentence into words. For example,<br />

function allWords = words(inputString)<br />

remainder = inputString;<br />

allWords = '';<br />

2-59

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!