This document discusses numeric data type conversion functions in MATLAB. It provides examples of functions that convert values to different numeric data types like double, single, int8, uint16 etc. It also discusses the smallest and largest integers and floating point numbers representable in MATLAB. Additionally, it covers strings as character arrays, converting between character codes and strings, and creating rectangular character arrays and cell arrays of strings.
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 ratings0% found this document useful (0 votes)
55 views12 pages
Matlab Introduction 5
This document discusses numeric data type conversion functions in MATLAB. It provides examples of functions that convert values to different numeric data types like double, single, int8, uint16 etc. It also discusses the smallest and largest integers and floating point numbers representable in MATLAB. Additionally, it covers strings as character arrays, converting between character codes and strings, and creating rectangular character arrays and cell arrays of strings.
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/ 12
Conversion to Various Numeric Data Types
Function Purpose
double Converts to double precision number
single Converts to single precision number
int8 Converts to 8-bit signed integer
int16 Converts to 16-bit signed integer
int32 Converts to 32-bit signed integer
int64 Converts to 64-bit signed integer
uint8 Converts to 8-bit unsigned integer
uint16 Converts to 16-bit unsigned integer
uint32 Converts to 32-bit unsigned integer
uint64 Converts to 64-bit unsigned integer
Example Smallest and Largest Integers • The functions intmax() and intmin() return the maximum and minimum values that can be represented with all types of integer numbers. • Both the functions take the integer data type as the argument, for example, intmax(int8) or intmin(int64) and return the maximum and minimum values that you can represent with the integer data type. Smallest and Largest Floating Point Numbers • The functions realmax() and realmin() return the maximum and minimum values that can be represented with floating point numbers. Strings • MATLAB considers all variables as arrays, and strings are considered as character arrays. • Interestingly, you can use numeric conversion functions like uint8() or uint16() to convert the characters in the string to their numeric codes. • The char() function converts the integer vector back to characters.
>> my_string = ‘ My name is’;
>> whos >> str_ascii = uint8(my_string) >> str_back_to_char= char(str_ascii) Rectangular Character Array • The strings discussed so far are one-dimensional character arrays; however, we need to store more than that. We need to store more dimensional textual data in our program. This is achieved by creating rectangular character arrays. • Simplest way of creating a rectangular character array is by concatenating two or more one-dimensional character arrays, either vertically or horizontally as required. • Using the MATLAB concatenation operator [] and separating each row with a semicolon (;). • Please note that in this method each row must contain the same number of characters. For strings with different lengths, you should pad with space characters as needed. Rectangular Character Array Rectangular Character Array • Using the char function. If the strings are of different lengths, char pads the shorter strings with trailing blanks so that each row has the same number of characters. Rectangular Character Array • You can combine strings horizontally in either of the following ways • Using the MATLAB concatenation operator, [] and separating the input strings with a comma or a space. This method preserves any trailing spaces in the input arrays. • Using the string concatenation function, strcat. This method removes trailing spaces in the inputs. Combining Strings into a Cell Array • From our previous discussion, it is clear that combining strings with different lengths could be a pain as all strings in the array has to be of the same length. We have used blank spaces at the end of strings to equalize their length. • However, a more efficient way to combine the strings is to convert the resulting array into a cell array. String Functions String Functions