How can I open, edit, and save a .dat file in MATLAB?

22 views (last 30 days)
Hello, I was wondering if anyone could help me with the following question:
I want to run a Monte Carlo type simulation. My sim calls initial conditions from a .dat file. I want to be able to open the IC.dat file, edit only one line of code (say, the weight), and then save it before calling the simulation. Any thoughts?
  1 Comment
dpb
dpb on 4 Sep 2018
Edited: dpb on 4 Sep 2018
Yeah, just use the builtin editor and make the change -- you're going to have to enter the value, anyway by this route.
Otherwise, redesign the input so that you can just pass a named parameter when you start the simulation that overwrites the parameter of same name in the input file.
Of course, for documentation purposes, your simulation needs to report the input conditions for the run together with its output.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 4 Sep 2018
".dat" is an extension used for many different data file formats, some of them text based and some of them binary and some of them a mix of text and binary.
For binary files, you can fopen() the file with 'a+' permission, and fseek() to the appropriate location, and fwrite() a new binary value and then fclose().
For text files, often the easiest way to proceed is to fileread() the entire file, regexprep() to locate and replace the appropriate string, then fopen/fwrite/fclose to write the new version of the file.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!