From: Kirk on
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
From: Jan Simon on
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
From: Kirk on
"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hsjl4t$g0v$1(a)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?
From: us on
"Kirk" <kwythers.nospam(a)umn.edu> wrote in message <hsjien$ht5$1(a)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
From: Jan Simon on
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