How to replace specific characters in a cell array?

187 visualizaciones (últimos 30 días)
Ivan Mich
Ivan Mich el 2 de Mzo. de 2023
Editada: Stephen23 el 18 de Mzo. de 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'}.
Could you please help me?
  2 comentarios
the cyclist
the cyclist el 2 de Mzo. de 2023
Can you give a small example of input and desired output, not just a description? When folks say "string", they often mean "character array", and it matters to the solution.
Ivan Mich
Ivan Mich el 4 de Mzo. de 2023
Editada: Ivan Mich el 4 de Mzo. de 2023
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'}.
Could you please help me?

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 4 de Mzo. de 2023
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'}
  3 comentarios
Ivan Mich
Ivan Mich el 12 de Mzo. de 2023
I solved it. Thank you
Voss
Voss el 12 de Mzo. de 2023
Editada: Voss el 12 de Mzo. de 2023
B = {'MATHIEW SAMOURAI'};
B = regexprep(B,'IEW\>','YAU')
B = 1×1 cell array
{'MATHYAU SAMOURAI'}

Iniciar sesión para comentar.

Más respuestas (3)

the cyclist
the cyclist el 4 de Mzo. de 2023
Editada: the cyclist el 4 de Mzo. de 2023
With the additional description you added that clarified the problem, you can do this very easily with regexprep:
A = {'MATHIEW','NIKOLIEW','SAMIEW','SAMOURAI','IEWBLAHIEW'};
B = regexprep(A,'IEW$','YAU')
B = 1×5 cell array
{'MATHYAU'} {'NIKOLYAU'} {'SAMYAU'} {'SAMOURAI'} {'IEWBLAHYAU'}
Note that this solution only replaces instances of IEW that occur at the end of the character array. (I added a case to illustrate that.)

the cyclist
the cyclist el 2 de Mzo. de 2023
Editada: the cyclist el 2 de Mzo. de 2023
Assuming you have character array elements, and not strings, here is one way:
% Inputs
CC_in = {'abcdeABCDE','xyzFGHIJ'};
replacementStr = '12345';
% Output
CC_out = cellfun(@(x) [x(1:end-5) replacementStr],CC_in,'UniformOutput',false)
CC_out = 1×2 cell array
{'abcde12345'} {'xyz12345'}
This could be generalized, for example to automatically read the length of the replacement string:
% Inputs
CC_in = {'abcdeABCDE','xyzFGHIJ'};
replacementStr = '12';
% Output
L = numel(replacementStr);
CC_out = cellfun(@(x) [x(1:end-L) replacementStr],CC_in,'UniformOutput',false)
CC_out = 1×2 cell array
{'abcdeABC12'} {'xyzFGH12'}
  2 comentarios
the cyclist
the cyclist el 2 de Mzo. de 2023
Just going to tag on to this answer, that there are other methods for strings. It might be more common for strings to be stored in a string array than a cell array. (This is arguably a better approach, if you do not have mixed datatypes contents to store.)
In that case, one can use replaceBetween:
% Inputs
CS = ["abcdeABCDE","xyzFGHIJ"];
replacementStr = "12345";
% Define the string lengths, for convenience
LCS = strlength(CS);
L = strlength(replacementStr);
% Output
CS_out = replaceBetween(CS,LCS-L,LCS,replacementStr)
CS_out = 1×2 string array
"abcd12345" "xy12345"
Stephen23
Stephen23 el 2 de Mzo. de 2023
Editada: Stephen23 el 3 de Mzo. de 2023
"This is arguably a better approach"
Agreed. In fact the MATLAB documentation is even stronger even than that, it specifically recommends against storing string scalars in a cell array:
"When you have multiple strings, store them in a string array, not a cell array. ... String arrays are more efficient than cell arrays for storing and manipulating text. ... Avoid using cell arrays of strings. When you use cell arrays, you give up the performance advantages that come from using string arrays."

Iniciar sesión para comentar.


Venkatanarasimha Hegde
Venkatanarasimha Hegde el 18 de Mzo. de 2023
Editada: Venkatanarasimha Hegde el 18 de Mzo. de 2023
A = {'MATHIEW','NIKOLIEW','SAMIEW','SAMOURAI','IEWBLAHIEW'};
for i = 1:length(A)
temp = A{i};
if strcmp(temp(end-2:end),'IEW')
temp(end-2:end) = 'YAU';
end
A{i} = temp;
end
disp(A)
{'MATHYAU'} {'NIKOLYAU'} {'SAMYAU'} {'SAMOURAI'} {'IEWBLAHYAU'}
This is a much simpler version using loops. Even beginners can try to implement such codes who don't know much about regexp or complicated constructs.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by