Find and Replace a specific String from excel work book

조회 수: 3 (최근 30일)
Akash Rana
Akash Rana 2019년 12월 18일
답변: Rahul 2025년 2월 19일
I have to find and replace a specific String from an excel work book which contains n number of sheets. The string is available in random location in different sheet's.Example I want to replace the string Rana_1 from all sheets with Akash_1 .One more thing format of the work book should not change. I tried regexp but it is not usefull .

답변 (1개)

Rahul
Rahul 2025년 2월 19일
I understand that you wish to replace centrain cells with a particular text with a different text. To achieve this, consider using:
  • This can be done by looping through each cell of the range of the excel sheet and checking as well as replacing the text in the cell with required text.
  • Another approach can be to read the data from the excel file using 'readcell' function and the apply the tranformation to all the cells using 'cellfun' with a custom helper function to replace the text. Then 'writecell' can be used to write the modified data back to the excel sheet. Here is an example:
FileName = 'yourFile.xlsx';
oldString = 'Rana_1';
newString = 'Akash_1';
% Read, modify and write the data
data = readcell(FileName);
modifiedData = cellfun(@(x) replaceIfMatch(x, oldString, newString), data, 'UniformOutput', false);
writecell(modifiedData, FileName);
% Helper function to replace matching strings
function output = replaceIfMatch(input, oldStr, newStr)
if ischar(input) && strcmp(input, oldStr)
output = newStr;
else
output = input;
end
end
Additionally, the following MATLAb Answer and File Exchange submission can be referred:
The following MathWorks documentations can be referred to know more:
Hope this helps! Thanks.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by