MATLAB: Can the program add code to itself

file iofile write

I'd like to add a sort of learning ability to my program where it recognizes new name inputs and adds them to my current list as a way of future proofing it. I would be adding an elseif statement at the same line every time (just after the initial if). My current idea as I'm assuming you can't write to a file while it is currently running in MATLAB is to set a value of 1 or 0 which will run another program afterward to write to the original if 1, or end if 0.
Is there a simpler way of writing to a specific line (in this case line 16) in a .m file?
Where can I find more information on how to go about this?

Best Answer

You can use fgetl() and fprintf() to write text to the m-file, as long as MATLAB doesn't have the m-file locked, which I don't think it does. I also agree with Stephen about that not being a wise approach, especially when better approaches are available.