Question
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'}.
Expert Answer
Kshitij Singh
PhD Expert
Answered Aug 25, 2026
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'}
100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related for loop Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →