MATLAB Answers


opening many files and changing code to match file name

Asked by John
on 18 Mar 2012


I was hoping that somebody might be able to help me with a problem that I have. I have 1000 files in the current directory called


I was wondering for each file how can I open it and change 2 lines in the code to be the same as the file name.

Here is an example

This file is called RW1.a_process

You will see RW1 in the two places marked red.

Is there anyway that I could do this for the remainder of the files? It would take me hours to do it manually.

Sincere thanks



Jiro Doke
on 18 Mar 2012

What do the 2 lines in the code have currently? I assume you have some placeholder text, and you want to change those according to the file name?

Rick Rosson
on 18 Mar 2012

Are the two lines of text always lines 2 and 15, or do they change location from one file to the next?

on 18 Mar 2012

Hello Jiro and Rick,
I'm using a software that is based on the Matlab environment. However, in order to run a simulation a process file is necessary.
What I did was, I copied an original process file a 1000 times and changed the file names to RW1.a_process ... RW1000.a_process.

The process files are all the same, except for the two parts that I've marked in red. The first part is just the display name in the program and the second corresponds to separate .mat file which contains data for the simulation. They are named RW1.mat...RW1000.mat

I could do it manually but since I have a 1000+ files it would take days to complete.
So just to confirm the two lines of text are always lines 2 and 15.

Jiro - I hope this answers your question too.

Thank you





No products are associated with this question.

2 Answers

Answer by Jiro Doke
on 18 Mar 2012
 Accepted answer

This is probably a little too specific to your case, i.e. it's looking for a particular text. I'm just using strrep but if it doesn't work, you may have to look at regexprep.

In general, I agree with Rick that you should do this from the beginning (using your original file), instead of creating 1000 copies, changing the file names, and then changing those 2 lines.

d = dir('*.a_process');
for id = 1:length(d)
    [p, name] = fileparts(d(id).name);
    fid = fopen(d(id).name, 'r');
    str = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', '');
    % Replace Strings
    newStr = strrep(str{1}, 'Run RW Cycle', ['Run ', name, ' Cycle']);
    newStr = strrep(newStr, 'RW.mat', [name, '.mat']);
    % Overwrite
    fid = fopen(d(id).name, 'w');
    fprintf(fid, '%s\n', newStr{:});

  1 Comment

on 6 Apr 2012

This works great!

Answer by Rick Rosson
on 18 Mar 2012

It may be easier to delete all of the files except for the original source file, and then use MATLAB to create all of the copies for you, but with the two specific lines customized on each copy.

Maybe, for example:

   for k = 2:1000
       fileName = ['RW'  num2str(k)];
       source = fopen( 'RW1.a_process','r');
       target = fopen([fileName '.a_process'],'w');
       Ln = 0;
       while ~feof(source)
           Ln = Ln + 1;
           aLine = fgetl(source);
           if Ln == 2
               aLine = sprintf(...);
               if Ln == 15
                   aLine = sprintf(...);

I realize that this code is not optimized, but it may help as a starting point.




Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!