Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
初级会员
注册日期: 2007-12-21
帖子: 2
声望力: 0 ![]() |
![]()
我用这段代码替换文件中的某些字符串的出现一个问题:替换后原文件中 “%”以及“%”后面(同一行的)信息丢掉了,估计是%在matlab中被当作注释处理掉了,请问有什么办法解决没有?多谢了
function findreplace(file,otext,ntext,varargin) %FINDREPLACE finds and replaces strings in a text file % % SYNTAX: % % findreplace(file,otext,ntext) % findreplace(file,otext,ntext,match) % % findreplace : This function finds and replaces strings in a text file % % file: text file name (with or without path) % otext: text to be replaced (old text) % ntext: replacing text (new text) % match: either (1) for match case or (0) to ignore case % default value is (1) % % Example: % findreplace('sample.txt','Moller','Moler'); % findreplace('sample.txt','jake','Jack',0); % findreplace('sample.txt','continue it is','continue its',0); % % Copyright 2005 Fahad Al Mahmood % Version: 1.0 $Date: 24-Dec-2005 % Obtaining the file full path [fpath,fname,fext] = fileparts(file); if isempty(fpath) out_path = pwd; elseif fpath(1)=='.' out_path = [pwd filesep fpath]; else out_path = fpath; end % Reading the file contents k=1; all=1; % replace all (Nono's comment.) It should be 0 for interative prompt. opt=[]; first_time=1; change_counter=0; fid = fopen([out_path filesep fname fext],'r'); while 1 line{k} = fgetl(fid); if ~ischar(line{k}) break; end k=k+1; end fclose(fid); old_lines = line; %Number of lines nlines = length(line)-1; for i=1:nlines if nargin==3, match=1; else match=varargin{1}; end if match==1, loc = regexp(line{i},otext); elseif match==0, loc = regexpi(line{i},otext); end if ~isempty(loc) nloc = 1; for j=1:length(loc) if all==0 % Displaying keyboard instructions if first_time disp(' '); disp('(y) change (n) skip (a) change all (s) stop'); disp(' '); first_time=0; end disp(line{i}); opt = input(underline(loc(j),length(otext)),'s'); if opt=='a' line{i} = regexprep(line{i},otext,ntext, 'preservecase',nloc); change_counter = change_counter + 1; all=1; if length(loc)>j loc(j:end) = loc(j:end) + (length(ntext)-length(otext)); end elseif opt=='y' line{i} = regexprep(line{i},otext,ntext, 'preservecase',nloc); change_counter = change_counter + 1; if length(loc)>j loc(j:end) = loc(j:end) + (length(ntext)-length(otext)); end elseif opt=='s'; break; else nloc = nloc + 1; end else line{i} = regexprep(line{i},otext,ntext, 'preservecase',nloc); change_counter = change_counter + 1; if length(loc)>j loc(j:end) = loc(j:end) + (length(ntext)-length(otext)); end end end end if opt=='s'; break end end line = line(1:end-1); disp(' '); if change_counter~=0 % Writing to file fid2 = fopen([out_path filesep fname fext],'w'); for i=1:nlines fprintf(fid2,[line{i} '\n']); end fclose(fid2); disp([num2str(change_counter) ' Changes Made & Saved Successfully']); disp(' '); else disp('No Match Found / No Change Applied'); disp(' '); end function uline = underline(loc,length) s=' '; l='-'; uline=[]; if loc==1 for i=1:length uline=[uline l]; end else for i=1:loc-1 uline = [uline s]; end for i=1:length uline=[uline l]; end end uline = [uline s]; |
![]() |
![]() |
![]() |
#2 |
普通会员
注册日期: 2007-12-02
年龄: 44
帖子: 74
声望力: 19 ![]() |
![]()
没有出现楼主所说的问题。
x.txt function findreplace(file,otext,ntext,varargin) %FINDREPLACE finds and replaces strings in a text file % % SYNTAX: % % findreplace(file,otext,ntext) % findreplace(file,otext,ntext,match) % % findreplace : This function finds and replaces strings in a text file % % file: text file name (with or without path) % otext: text to be replaced (old text) % ntext: replacing text (new text) % match: either (1) for match case or (0) to ignore case % default value is (1) % >> findreplace('D:\x.txt','%','a') 15 Changes Made & Saved Successfully 处理得到的结果: function findreplace(file,otext,ntext,varargin) aFINDREPLACE finds and replaces strings in a text file a a SYNTAX: a a findreplace(file,otext,ntext) a findreplace(file,otext,ntext,match) a a findreplace : This function finds and replaces strings in a text file a a file: text file name (with or without path) a otext: text to be replaced (old text) a ntext: replacing text (new text) a match: either (1) for match case or (0) to ignore case a default value is (1) a |
![]() |
![]() |
![]() |
#3 |
初级会员
注册日期: 2007-12-21
帖子: 2
声望力: 0 ![]() |
![]()
版主试试这句:
findreplace('aaaa.txt','text','test') 结果只剩下第一行了 |
![]() |
![]() |
![]() |
#4 |
初级会员
注册日期: 2012-07-08
年龄: 35
帖子: 6
声望力: 0 ![]() |
![]()
英文来的看不懂!
SIGNATURE............................................................................................................................................................ 丝杆|http://www.gunzhusigan.com/ |
![]() |
![]() |