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

Lecture10 Octave Strings

The document provides an overview of string constants in Octave, explaining how they can be created, concatenated, and manipulated using various functions. It details escape sequences, string functions, and the differences between string arrays and character arrays. Additionally, it covers output display methods and pagination for lengthy outputs in the Command Window.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture10 Octave Strings

The document provides an overview of string constants in Octave, explaining how they can be created, concatenated, and manipulated using various functions. It details escape sequences, string functions, and the differences between string arrays and character arrays. Additionally, it covers output display methods and pagination for lengthy outputs in the Command Window.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Octave:

strings
Strings
 A string constant consists of a sequence of characters enclosed in either double-
quote or single-quote marks. For example, both of the following expressions
"parrot“ 'parrot’ represent the string whose contents are parrot.

 Strings in Octave can be of any length.


 Since the single-quote mark is also used for the transpose operator (see
Arithmetic Ops) but double-quote marks have no other purpose in Octave, it is
best to use double-quote marks to denote strings.
 Strings can be concatenated using the notation for defining matrices – e.g
expression: [ "foo" , "bar" , "baz"] produces string whose contents are ‘foobarbaz’.
 Escape Sequences in String Constants
 In double-quoted strings, the backslash character is used to introduce escape
sequences that represent other characters. For example, ‘\n’ embeds a newline
character in a double-quoted string and ‘\"’ embeds a double quote character. In
single-quoted strings, backslash is not a special character. Here is an example
showing the difference:
double ("\n")
⇒ 10
double ('\n')
⇒ [ 92 110 ]

© Prof Suvendi Rimer, UJ


table of all the escape sequences used in Octave
 \\ - represents a literal backslash, ‘\’.
 \“ - represents a literal double-quote character, ‘"’.
 \‘ - represents a literal single-quote character, ‘'’.
 \0 - represents the null character, control-@, ASCII code 0.
 \a - represents the “alert” character, control-g, ASCII code 7.
 \b - represents a backspace, control-h, ASCII code 8.
 \f - represents a formfeed, control-l, ASCII code 12.
 \n - represents a newline, control-j, ASCII code 10.
 \r - represents a carriage return, control-m, ASCII code 13.
 \t - represents a horizontal tab, control-i, ASCII code 9.
 \v - represents a vertical tab, control-k, ASCII code 11.
 In a single-quoted string there is only one escape sequence: may insert a single
quote character using two single quote characters in succession. For example,
'I can''t escape'
⇒ I can't escape

© Prof Suvendi Rimer, UJ


String Functions
 In scripts the two different string types can be distinguished if necessary by using
is_dq_string and is_sq_string.
 is_dq_string (x): Return true if x is a double-quoted character string.
 is_sq_string (x): Return true if x is a single-quoted character string.
 The function ischar can be used to test if an object is a character matrix.
 ischar (x): Return true if x is a character array.
 isstring (s): Return true if s is a string array.
 A string array is a data type that stores strings (row vectors of characters) at each
element in the array. It is distinct from character arrays which are N-dimensional
arrays where each element is a single 1x1 character. It is also distinct from cell
arrays of strings which store strings at each element, but use cell indexing ‘{}’ to
access elements rather than string arrays which use ordinary array indexing ‘()’.
 To test if an object is a string (i.e., a 1xN row vector of characters and not a
character matrix) - use ischar function in combination with isrow function
ischar (collection)
⇒1
ischar (collection) && isrow (collection)
⇒0
ischar ("my string") && isrow ("my string")
⇒1
© Prof Suvendi Rimer, UJ
String Functions
 Creating Strings: enclose a text in double-quotes or single-quotes. Possible to
create a string without actually writing a text. The function blanks creates a string
of a given length consisting only of blank characters –
 blanks (n) - Return a string of n blanks.
 Concatenating Strings: Strings can be concatenated using matrix notation
 Example: fullname = [fname ".txt"]; email = ["<" user "@" domain ">"];
 In each case it is easy to see what the final string will look like. This method is
also the most efficient. When using matrix concatenation the parser immediately
begins joining the strings without having to process the overhead of a function
call and the input validation of the associated function.
 The newline function can be used to join strings such that they appear as multiple
lines of text when displayed: newline. Equivalent to "\n".
 Example: joined_string = [newline "line1" newline "line2"]
 Other functions for concatenating string objects:char, strvcat, strcat, and cstrcat.
 char and strvcat concatenate vertically, while strcat and cstrcat concatenate
horizontally.
 Examples: char ("an apple", "two pears"), strcat ("oc", "tave", " is", " good", " for you")

© Prof Suvendi Rimer, UJ


Character Strings
 Character strings in Octave are special numerical arrays of ASCII values that are
displayed as their character-string representation.
 A character string is simply text surrounded by single quotes.
 Each character in a string is one element in an array.
 Single quotes within a character string are symbolized by two consecutive
quotes.
 The functions char and strvcat create multiple-row strings from individual strings
of varying lengths.
 Strings can be manipulated with all the array manipulation tools available in
Octave.

© Prof Suvendi Rimer, UJ


Displaying Output data
 The disp function accepts an array argument and displays the value of the array
in the Command Window.
 If the array is of type char, then the character string contained in the array is
printed out.
 This function is often combined with the functions num2str (convert non-integer
arrays to a string) and int2str (convert integer arrays to a string) to create a
message to be displayed in the Command Window:
» str=['The value of pi = ' num2str(pi)];
» disp(str);

 The value of pi = 3.1416


 If output in the Command Window is lengthy, it might not fit within the screen and
will display too quickly for you to see it.
 Use the more function to control the paging of output in the Command Window.
 By default, more is off. When you type more on, Octave displays only a page (a
screen full) of output at a time.
 After the first screen displays, press one of the following keys:
 ENTER : To advance to the next line
 SPACE : To advance to the next page
 q : To stop displaying the output.
© Prof Suvendi Rimer, UJ

You might also like