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

조회 수: 10 (최근 30일)
Mike McGivern
Mike McGivern 2018년 9월 4일
편집: dpb 2018년 9월 4일
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