MATLAB Answers

How can I replace a specific number (row/column) in .dat file

22 views (last 30 days)
Alexandre on 10 Jul 2014
Commented: Alexandre on 11 Jul 2014
I need to use a .dat as an input, so I would like to replace specific row/column element (number) for each iteration. This element is located in the first column in which are numbers and letters (depending on the row), the other columns have only letters.
Do I need to change the file format to another one (.txt for instance), then make the replacement, and finally convert it to .dat again? Which options do I have?
I tried dlmwrite, fwrite and many others but unsuccessfully.
Thank you very much.

Answers (1)

Laurent on 10 Jul 2014
It depends on how your data is stored in the .dat file.
If it is ASCII, you can load it using
Then you can do your replacements and save it using the save command.
Alexandre on 11 Jul 2014
Thanks again Laurent!I see that this code alows to load the file (I can see the numbers) but it seems that I am losing the original format of the .dat file, isn't? I say that because if, let's say, I can replace the desired number in the loaded file, then will I be able convert it to a .dat file with the original format only with a number replaced?
To be more accurated this would be a part of my .dat file (opened with notepad). And I want to replace the number of the last two lines in order to work as a new input. Is it really possible?
Thank youy very much!
------- HydroDyn v2.01.* Input File ------------------------
NREL 5.0 MW offshore baseline floating platform input properties
False Echo - Echo the input file data (flag)
---------------------- ENVIRONMENTAL CONDITIONS -----------
1027 WtrDens - Water density (kg/m^3)
20 WtrDpth - Water depth (meters)
0 MSL2SWL - Offset between s
---------------------- WAVES -----------------------------
2 WaveMod - Incident
0 WaveStMod - Model for
3630 WaveTMax - Analysis
0.25 WaveDT - Time step f
6 WaveHs - Significant
10 WaveTp - Peak-spectral
"DEFAULT" WavePkShp - Peak-shape spectrum
0 WvLowCOff - Low cut-off
500 WvHiCOff - High cut-off frequency
0 WaveDir - Incident wave
0 WaveDirMod - Directional
1 WaveDirSpread - Wave direction
1 WaveNDir - Number of wave directions
90 WaveDirRange - Range of wave directions
123456789 WaveSeed(1) - First random seed of incident waves
1011121314 WaveSeed(2) - Second random seed of incident waves [

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!