How to replace specific characters in a cell array?

Technical Source
1 min readMar 14, 2023

--

I have a question about a code.

I have a cell array with strings and I would like to replace the last 5 characters with another for every cell array. I tried strrep but no use.

For example I have an array:

A={‘MATHIEW’,’NIKOLIEW’,’SAMIEW’,’SAMOURAI’}.

I would like to replace ONLY in the strings that their last 3 characters have the elen\ments: IEW, with the elements ‘YAU’.

I mean that I want my final array to be:

B={‘MATHYAU’,’NIKOLYAU’,’SAMYAU’,’SAMOURAI’}.

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

Try the function endsWith in a loop to check if the string ends with certain characters

B={'MATHIEW','NIKOLIEW','SAMIEW','SAMOURAI'}
B = 1×4 cell array
{'MATHIEW'} {'NIKOLIEW'} {'SAMIEW'} {'SAMOURAI'}
for k = 1 : numel(B)
if endsWith(B{k}, 'IEW', 'IgnoreCase', true)
B{k}(end-2 : end) = 'YAU';
end
end
B % Show in command window
B = 1×4 cell array
{'MATHYAU'} {'NIKOLYAU'} {'SAMYAU'} {'SAMOURAI'}

SEE COMPLETE ANSWER CLICK THE LINK

--

--

Technical Source
Technical Source

Written by Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.

No responses yet