Search and replace a word of text file from GUI
조회 수: 1 (최근 30일)
이전 댓글 표시
I need to parse a text file in way like what find and replace (strrep) function does in the string. It actually has to get a word (‘new’) as input from the GUI, search within the text file for a special word (‘old’) and replace it.
Any help would be greatly appreciated
댓글 수: 0
채택된 답변
Stephen23
2016년 2월 29일
편집: Stephen23
2016년 2월 29일
str = fileread(filename);
str = strrep(str,old,new);
댓글 수: 7
Stephen23
2016년 3월 3일
편집: Stephen23
2016년 3월 3일
Reading the inputdlg documentation we find that the output is a cell array. strrep also accepts cell arrays of strings, and then returns a cell array. Oops, providing a cell array to fprintf results in this error message:
Error using fprintf
Function is not defined for 'cell' inputs.
Error in bj (line 9)
fprintf(fid,'%s',str);
By the point this error occurs the file has already been opened for writing (and the contents deleted). To solve this we just need to get the string out of the cell array, using cell array indexing:
str = strrep(str,'old',new{1});
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!