0% found this document useful (0 votes)
20 views

Regular Expression and Cell Array1

Regular expressions can define patterns to match text strings. Cell arrays allow storing different data types in indexed containers. Regular expressions are used to search cell arrays containing text and return matches as cell arrays. Functions like regexp perform regular expression matching and regexprep replaces matches. Cell arrays can be accessed, modified, and converted to and from numeric arrays using functions like cell2mat, mat2cell, and num2cell.

Uploaded by

deepak joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Regular Expression and Cell Array1

Regular expressions can define patterns to match text strings. Cell arrays allow storing different data types in indexed containers. Regular expressions are used to search cell arrays containing text and return matches as cell arrays. Functions like regexp perform regular expression matching and regexprep replaces matches. Cell arrays can be accessed, modified, and converted to and from numeric arrays using functions like cell2mat, mat2cell, and num2cell.

Uploaded by

deepak joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Regular Expression and Cell Array

Regular Expression
• A regular expression is a sequence of characters that defines a
certain pattern.
Example
character vector 'Joh?n\w*'
– It defines a pattern that starts with the letters Jo, is optionally
followed by the letter h (indicated by 'h?'), is then followed by the
letter n, and ends with any number of word characters, that is,
characters that are alphabetic, numeric, or underscore (indicated
by '\w*').
– This pattern matches any of the following:
Jon, John, Jonathan, Johnny
• str = 'bat cat can car coat court CUT ct CAT-scan';
expression = 'c[aeiou]+t';
startIndex = regexp(str,expression)

Output-
startIndex = 1×2
5 17
• text = ['The high-speed train traveled at 250 ', ... 'kilometers per
hour alongside the automobile ', ... 'travelling at 120 km/h.'];
pattern = 'k(ilo)?m(eters)?(/|\sper\s)h(r|our)?';
regexp(text, pattern, 'match')
• output
ans = 1×2
cell array {'kilometers per hour'} {'km/h'}

• Note- I symbol is used for OR operator.


expr? Means 0 times or 1 time.
\s is used for white space.
• str = 'EXTRA! The regexp function helps you relax.';
expression = '\w*x\w*';
matchStr = regexp(str,expression,'match')

Output
matchStr = 1x2
cell {'regexp'} {'relax'}

Note -
• \w means any alphabetic, numeric, or underscore character. For English,
\w is equivalent to [a-zA-Z_0-9]
• \W means any character that is not alphabetic, numeric, or underscore.
\W is equivalent to [^a-zA-Z_0-9]
• expr* 0 or more times consecutively. '\w*' matches a word of any length.
Functions

Function Description
regexp Match regular expression.
regexpi Match regular expression, ignoring case.
regexprep Replace part of text using regular expression.
regexptranslate Translate text into regular expression.

https://in.mathworks.com/help/matlab/matlab_prog/regular-expressions.html
Cell Array
• A cell array is a data type with indexed data containers
called cells, where each cell can contain any type of data.
• Cell arrays commonly contain either lists of text, combinations of
text and numbers, or numeric arrays of different sizes.
• C = {} # empty cell array
• C = cell(n) returns an n-by-n cell array of empty matrices.
• C= cell(2,3) returns a 2-by-3 cell array.
• C1 = {'2017-08-16',[56 67 78]}
output- C=1×2 cell array
{'2017-08-16'} {1x3 double}
Cell Array
• C1(2,:) = {'2017-08-17',[58 69 79]};
C1(3,:) = {'2017-08-18',[60 68 81]}
output
C1=3×2 cell array
{'2017-08-16'} {1x3 double}
{'2017-08-17'} {1x3 double}
{'2017-08-18'} {1x3 double}

C1(1,:)
output – ans = 1×2 cell array
{'2017-08-16'} {1x3 double}

C1{1,2}
output-
Cell Array
• A = [7 9; 2 1; 8 3];
C = cell(size(A))
output-
C=3×2 cell array
{0x0 double} {0x0 double}
{0x0 double} {0x0 double}
{0x0 double} {0x0 double}
3D Cell Array
• C = cell(3,4,2)
Output
Cell array to matrix
• A = cell2mat(C)
converts a cell array into an ordinary array. The elements of
the cell array must all contain the same data type, and the
resulting array is of that data type.

C = {[1], [2 3 4];
[5; 9], [6 7 8; 10 11 12]}
A = cell2mat(C)
A = 3×4
1 2 3 4
5 6 7 8
9 10 11 12
Matrix to cell array
• C = mat2cell(A,dim1Dist,...,dimNDist)
divides array A into smaller arrays and returns them in cell
array C. The vectors dim1Dist,...dimNDist specify how to
divide the rows, the columns, and (when applicable) the
higher dimensions of A.
The smaller arrays in C can have different sizes. A can have
any data type.
A = reshape(1:20,5,4)’
C3 = mat2cell(A,[2 2],[3 2])
Divide A into two 2-by-3 and
two 2-by-2 subarrays.
Access Data
• C 4= {'one', 'two', 'three'; 1, 2, 3}
• Update sets of cells by replacing them with the same number of cells.
C4(1,1:3) = {'first','second','third'}
output
C4=2×3 cell array
{'first'} {'second'} {'third'} {[ 1]} {[ 2]} {[ 3]}
• Access the contents of cells--the numbers, text, or other data within the
cells--by indexing with curly braces.
last = C4{2,3}
Output
last = 3
• Similarly, you can index with curly braces to replace the contents of a cell.
Adds Cells to Cell Array
• We can assign data to a cell outside the current dimensions.
C = {1, 2, 3}
C{4,4} = 44
output
C=4×4 cell array
{[ 1]} {[ 2]} {[ 3]} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {[ 44]}
• We can add cells without specifying a value.
• C{5,5} = []
C=5×5 cell array
Character array to cell array
• C = cellstr(A) converts A to a cell array of character vectors.
• A = ['abc ';'defg';'hi ']
C5 = cellstr(A)
output
Array to Cell Array
• C = num2cell(A) converts array A into cell array C by placing each
element of A into a separate cell in C.
• The num2cell function converts an array that has any data type—
even a nonnumeric type.
• Example 1
a = magic(3)
c = num2cell(a)
• Example 2
A1 = ['four';'five';'nine']
C1 = num2cell(a1)
C1 will be 3 x 4 cell.

You might also like