Loop for editing editing values in .dat file

5 views (last 30 days)
Dear all,
I am trying to write a script for automatically changing values into a .dat file,which is contained into a folder. Part of the .dat file is given below. . What I want to do, is to create a loop, where for each iteretation, the folder is copied and the .dat file in each copied folder is opened, and the value "ExampleValue" is assigned into a specific value for every iteration. My idea is the following. The folder is copied via the copyfile command. Then, the file is opened via fopen,read via fread, the string is replaced with the value I want via strrep and the the file closes via fclose and the loop continues. However, I have issues in reading the file. The file should be read line by line, without any format change. Does anyone have any idea on how to make the commands work?Or any other direction for reading the file?
Best Regards,
Ioannis Voultsos.
------- InflowWind v3.01.* INPUT FILE -------------------------------------------------------------------------
IEA 15 MW Offshore Reference Turbine
---------------------------------------------------------------------------------------------------------------
True Echo - Echo input data to <RootName>.ech (flag)
5 WindType - switch for wind file type (1=steady; 2=uniform; 3=binary TurbSim FF; 4=binary Bladed-style FF; 5=HAWC format; 6=User defined)
0 PropagationDir - Direction of wind propagation (meteoroligical rotation from aligned with X (positive rotates towards -Y) -- degrees)
0 VFlowAng - Upflow angle (degrees) (not used for native Bladed format WindType=7)
1 NWindVel - Number of points to output the wind velocity (0 to 9)
0.0 WindVxiList - List of coordinates in the inertial X direction (m)
0.0 WindVyiList - List of coordinates in the inertial Y direction (m)
150.0 WindVziList - List of coordinates in the inertial Z direction (m)
================== Parameters for Steady Wind Conditions [used only for WindType = 1] =========================
"ExampleValue" HWindSpeed - Horizontal windspeed

Accepted Answer

Image Analyst
Image Analyst on 21 Jan 2022
Here's a start:
% Open the file for reading in text mode.
inputFileID = fopen(fullInputFileName, 'rt');
% Open the file for writing in text mode.
outputFileID = fopen(fullOutputFileName, 'wt');
if inputFileID ~= -1 && outputFileID ~= -1
exampleValue = 123.456; % Whatever........
% Read the first line of the file.
textLine = fgetl(inputFileID);
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
if contains(textLine, 'HWindSpeed')
% Found the line we're looking for. Now create it with the new value.
textLine = sprintf('%.3f HWindSpeed - Horizontal windspeed ', exampleValue);
end
% Write the line to the output file.
fprintf(outputFileID, '%s\n', textLine);
% Read the next line.
textLine = fgetl(inputFileID);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the file.
fclose(inputFileID);
fclose(outputFileID);
end

More Answers (1)

Ioannis Voultsos
Ioannis Voultsos on 23 Jan 2022
It works perfectly and it does exactly what I wanted. Thank you very much!

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!