How can I extract the first 4 or the middle 4 or last four letters of a string example: when the string reads 01 ED 01 F9 81 C6?
newStr = extract( str , pat ) returns any substrings in str that match the pattern specified by pat . If str is a string array or a cell array of character vectors, then the function extracts substrings from each element of str .
You can index into, reshape, and concatenate string arrays using standard array operations, and you can append text to them using the + operator.
The escape character in Matlab is the single quote ('), not the backslash (\), like in C language.
%s represents character vector(containing letters) and %f represents fixed point notation(containining numbers). In your case you want to print letters so if you use %f formatspec you won't get the desired result.
A string is treated like a vector of chars. Try this:
>> string = '01 ED 01 F9 81 C6';
>> string(1:5), string(6:11), string(12:17)
ans =
01 ED
ans =
01 F9
ans =
81 C6
string
in this example is a variable not a method. string(1)
returns the first char in the array (or vector) called string
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With