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

22 次查看(过去 30 天)
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 个评论
dpb
dpb 2018-9-4
编辑:dpb 2018-9-4
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.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2018-9-4
".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.

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by