niloofar
niloofar

Reputation: 1

reading some part of string from cell array in MATLAB

I want to copy some part of a string from a cell array and put it into another cell array in MATLAB.

For example, one struct is like this

{>eco:b0002 thrA; Bifunctional aspartokinase/homoserine dehydrogenase 1 (EC:1.1.1.3 2.7.2.4); K12524 bifunctional aspartokinase / homoserine dehydrogenase 1 [EC:2.7.2.4 1.1.1.3] (N) atgcgagtgttgaa...}, 

I want to paste the

'>eco:b0002 thrA; Bifunctional aspartokinase/homoserine dehydrogenase 1 (EC:1.1.1.3 2.7.2.4); K12524 bifunctional aspartokinase / homoserine dehydrogenase 1 [EC:2.7.2.4 1.1.1.3] (N)' 

part of referring string into another cell array.

In each cell it begins with '>' and ends up with '(N)', as you see one of them in the example.

I can't find any helpful function to start with.

Upvotes: 0

Views: 74

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112659

>> str = '>eco:b0002 thrA; (N) atgcgagtgttgaa...';
>> result = regexp(str, '\>.+\(N\)', 'match');
>> result = result{1}
result =
>eco:b0002 thrA; (N)

Upvotes: 1

Tobia Tesan
Tobia Tesan

Reputation: 1936

You want to use a regular expression.

Try for example:

> str = "> fooooo (N) bar baz"
> exp = "^>(.*)\\(N\\).*$"
> [tokens, matches] = regexp(str, exp, 'tokens', 'match')
> tokens{1}{1}
ans => foooooo

(Disclaimer: I tried the above in Octave, which should behave exactly the same).

Upvotes: 0

Related Questions