find/replace line of code (with carriage return)

  • Follow


I have a large m-file in which I need to find 1 line of code, then replace it with two lines of code that contain a carriage return separating them. Is this possible? The goal is to add an additional line of code in several hundred places in the m-file. Therefore that replace code will contain the original line of code plus the additional line of code.

Thanks in advance
0
Reply Kirk 5/14/2010 1:16:07 PM

Dear Kirk!

> I have a large m-file in which I need to find 1 line of code, then replace it with two lines of code that contain a carriage return separating them. Is this possible? The goal is to add an additional line of code in several hundred places in the m-file. Therefore that replace code will contain the original line of code plus the additional line of code.

This should work with the replace-function all modern editors, e.g. Matlab's built-in editor.

Another approach:
  T = testread(FileName, '%s', 'delimiter', '\n');
  T(strcmp(T, 'searched line')) = {['line1', char(10), 'line2'};
  FID = fopen(FileName, 'wb');
  if FID < 0, error('Cannot open file.'); end
  fprintf(FID, '%s\n', T{:});
  fclose(FID);

If surrounding blanks will impede this, you can use this:
  T = testread(FileName, '%s', 'delimiter', '\n');
  S = strtrim(T);
  Index = strcmp(S, 'searched line');
  T(Index) = {['line1', char(10), 'line2'};
  ... as above
Then let Matlab's editor indent the complete function text automatically.

Good luck, Jan
0
Reply Jan 5/14/2010 2:02:05 PM


"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hsjl4t$g0v$1@fred.mathworks.com>...
> Dear Kirk!
> 
> > I have a large m-file in which I need to find 1 line of code, then replace it with two lines of code that contain a carriage return separating them. Is this possible? The goal is to add an additional line of code in several hundred places in the m-file. Therefore that replace code will contain the original line of code plus the additional line of code.
> 
> This should work with the replace-function all modern editors, e.g. Matlab's built-in editor.
> 
> Another approach:
>   T = testread(FileName, '%s', 'delimiter', '\n');
>   T(strcmp(T, 'searched line')) = {['line1', char(10), 'line2'};
>   FID = fopen(FileName, 'wb');
>   if FID < 0, error('Cannot open file.'); end
>   fprintf(FID, '%s\n', T{:});
>   fclose(FID);
> 
> If surrounding blanks will impede this, you can use this:
>   T = testread(FileName, '%s', 'delimiter', '\n');
>   S = strtrim(T);
>   Index = strcmp(S, 'searched line');
>   T(Index) = {['line1', char(10), 'line2'};
>   ... as above
> Then let Matlab's editor indent the complete function text automatically.
> 
> Good luck, Jan

Thanks Jan. I was trying to just use '\n' within the search and replace string in MATLAB's  FIND AND REPLACE gui. No success however. Looks like your approach is to be run as an m-file?
0
Reply Kirk 5/14/2010 2:22:05 PM

"Kirk" <kwythers.nospam@umn.edu> wrote in message <hsjien$ht5$1@fred.mathworks.com>...
> I have a large m-file in which I need to find 1 line of code, then replace it with two lines of code that contain a carriage return separating them. Is this possible? The goal is to add an additional line of code in several hundred places in the m-file. Therefore that replace code will contain the original line of code plus the additional line of code.
> 
> Thanks in advance

one of the many solutions

     fnam='pi.m';     % <- your file...
     s=textread(fnam,'%s','delimiter','\n');
     ss=repmat({''},2*size(s,1),1);
     ss(1:2:end)=s;

us
0
Reply us 5/14/2010 3:03:06 PM

Dear Kirk!

> Thanks Jan. I was trying to just use '\n' within the search and replace string in MATLAB's  FIND AND REPLACE gui. No success however. Looks like your approach is to be run as an m-file?

You could create the 2 lines in the editor and insert it in the replace-dialog by copy and paste --- at least I thought it would work in Matlab's editor. But it doesn't - what a pitty.
But you can use another editor for that. 

Yes, the code I've posted is Matlab code, which can run in an M-function, M-script or from the command line.

Jan
0
Reply Jan 5/14/2010 3:13:05 PM

> You could create the 2 lines in the editor and insert it in the replace-dialog by copy and paste --- at least I thought it would work in Matlab's editor. But it doesn't - what a pitty.

That would be neat if it did. I think it is an oversight not to allow for some special characters (carriage returns, etc.) or even better, regular expressions to work with Matlab's text editor. 
0
Reply Kirk 5/14/2010 8:19:04 PM

"Kirk" <kwythers.nospam@umn.edu> wrote in message <hskb7o$7cc$1@fred.mathworks.com>...
> > You could create the 2 lines in the editor and insert it in the replace-dialog by copy and paste --- at least I thought it would work in Matlab's editor. But it doesn't - what a pitty.
> 
> That would be neat if it did. I think it is an oversight not to allow for some special characters (carriage returns, etc.) or even better, regular expressions to work with Matlab's text editor. 

I sometimes solve these kinds of problems with Notepad++ in Windows, Geany in Linux.

http://www.geany.org/manual/current/images/replace_dialog.png
0
Reply Alan 5/14/2010 9:16:04 PM

6 Replies
671 Views

(page loaded in 0.04 seconds)

Similiar Articles:













7/23/2012 10:19:52 AM


Reply: