MATLAB: Create a MATLAB program that creates other programs

create .m files

Hi there,
I would like to create 100 MATLAB programs that are identical except for one line of code.
My reason for doing this (rather than including everything in a single program) is because the way my school's server works, I need to submit separate programs in order for them to all run at the same time (each on their own processor).
So, I was wondering if there is a way to create a single MATLAB program which outputs other MATLAB programs (.m files).
Thanks!

Best Answer

Here is one solution. Define two files:
  1. Write a "template" Mfile, which is complete except for that line. On that line put a unique string, such as XXX.
  2. Write another "lines" file, containing every version of that replacement line that you want to use.
Then in a script, do these steps in a loop:
  1. Read one line of the "lines" file, e.g. newline = fgetl(..).
  2. Read the template file into a variable, e.g. str = fileread(..).
  3. Use string functions to find and replace that line, e.g. strrep(str,'XXX',newline).
  4. save the changed variable in a new file (remember it needs a new name).
Bingo! You will have all of your Mfiles /functions, identical except for that one line.