2

I have a cell-array C with 7 strings inside C as follows:

 C =  { 'ABCDF'
'ABF'
'ABCDEFG'
'ABCDEF'
'ABDEFG'
'ABCDEFG'
'ABCEG' }

How can I change the 'AB' in each of the 3 first strings into 'BA'? The remain strings just keep the same as the original. The expected output will be as follows:

C =  { 'BACDF'
      'BAF'
      'BACDEFG'
      'ABCDEF'
      'ABDEFG'
      'ABCDEFG'
       'ABCEG' }
1
  • Did you try writing a loop? strfind might be useful. Commented Dec 6, 2015 at 16:55

1 Answer 1

4

That is exactly what regexprep

C(1:3) = regexprep(C(1:3),'AB','BA')

or strrep does:

C(1:3) = strrep(C(1:3),'AB','BA')

C = 

    'BACDF'
    'BAF'
    'BACDEFG'
    'ABCDEF'
    'ABDEFG'
    'ABCDEFG'
    'ABCEG'
Sign up to request clarification or add additional context in comments.

1 Comment

Also strrep(C,'AB','BA')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.