match a word/letter and add to the string cell - without a for loop

1 view (last 30 days)
Hi, I would like to check for a letter in a cell string and if it matches the letter I would like to add a word or a sentence to the string eg:
Initial cell:
P etc etc etc
A etc etc etc
P etc etc etc
A etc etc etc
Final Cell
aaa P etc etc etc
A etc etc etc
aaa P etc etc ect
A etc ect ect
My appologies I know this is a horible example.
However, I would like to accomplish this without using a for loop. I've tried to use cellfun but I couldnt figure out how to do it. Thank you
Vincent I
Vincent I on 26 Aug 2013
Azzi: What is the size of your initial cell? are you looking for matching letter in the first column or in all the cell array?
The size of the initial cell could change. Yes, I am only looking for matching the first latter in the cell array.

Sign in to comment.

Accepted Answer

Jan on 26 Aug 2013
Edited: Jan on 26 Aug 2013
Unfortunately the contents of your data is still not clear, although there have been questions for clarifications.
If you have:
C = {'P etc etc etc'; ...
'A etc etc etc'; ...
'P etc etc etc'; ...
'A etc etc etc'}
This might help:
index = strncmp(C, 'P', 1);
C(index) = strcat('aaa', C(index));
But if you have a different input, please explain this preferably by adding valid Matlab code, which creates the data exactly. The original question is the best place for such details, so take the chance to edit it instead of hiding important details in comments.

More Answers (0)


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!