0
votes

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.

2

2 Answers

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

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