Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract first 4 letters from a string in matlab

Tags:

string

matlab

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?

like image 537
Dilip Avatar asked Apr 04 '11 22:04

Dilip


People also ask

How do you extract part of a string in Matlab?

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 .

Can you index a string in Matlab?

You can index into, reshape, and concatenate string arrays using standard array operations, and you can append text to them using the + operator.

What is escape character in Matlab?

The escape character in Matlab is the single quote ('), not the backslash (\), like in C language.

What is %s in Matlab?

%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.


1 Answers

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.

like image 106
trolle3000 Avatar answered Oct 02 '22 22:10

trolle3000