Loop for editing editing values in .dat file
5 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Image Analyst
2022 年 1 月 21 日
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
0 件のコメント
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Data Import and Export についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!