Handling text value and characters in MATLAB
As discussed in the previous article, a text value can also be assigned to a variable. The string variable used for input is identified as text & character variable in MATLAB. In the example below, a string or a sequence of characters are enclosed in single quotes.
If the text value itself contains single quote; eg. Sam’s in the image 2 below, then three single quotes are used within the statement. Two for text and one for the apostrophe (See Image 2 below).
MATLAB stores data in array and matrix format. In case of string, data is stored in the form of an array. Once the string variable is stored in an array it becomes string array. Two string arrays can be concatenated or joined together using square brackets. In the following example (Image 3), two strings that is string stored in “d” i.e. hello and “text” i.e. ‘This is Sam’s file’ are joined together.
Converting numeric values to string values
A numeric value can be converted to string using commands like numeric to string. It can be done using the “num2str (Argument)” or integer to string i.e. “int2str (Argument)”.
For example in Image 4, variable “x” that holds a numeric value that is 2 is converted to string and used in the statement “hello, Elle turned “2” yesterday”.
Quick Tip: As Image 4 shows operation “x=u-v” is followed by a semicolon. This is not done to process the operation but stop the results from getting displayed. This is because many a times not all information is displayed in the output.
Calling functions
Functions are pre-defined modules of code that once created can be used again and again just by their names instead of writing the whole code. Thus whenever a code of program is required, its function is “called” Kanetkar, (2016). For example, if a variable is to be displayed, then instead of writing the whole code for displaying the code, its corresponding function “disp()” is called. A function can be called by using parenthesis for entering input arguments.
For example: consider “num2str()” function in Image 4 above, the value of x was called and returned as string in the result.
Multiple argument: In case of multiple arguments, comma is used to separate the calling functions (Image 5):
Assigning function output: Output of a function can also be returned by assigning it to a variable (Image 6)
Multiple output arguments: In the above example minimum is the name of the variable in which the value returned from function “min()” is stored. In case there are multiple output arguments, then square brackets are used to enclose them as per the syntax defined in MATLAB (Image 7)
In the above example, value returned by the function “min(B)”is assigned to variable “cat” and the location or position of the outcome of “min(B)” that is number 22 in array B is returned.
In case, function is using string argument, it is to be projected (‘hey’) in single quotes (Image 8).
Reference
- Kanetkar, Y. P. (2016). Let Us C (14th ed.). BPB Publications.
Discuss