Read values from headers in txt file as parameters, as well as separately reading data matrix from the rest of the file.
1 回表示 (過去 30 日間)
古いコメントを表示
Hello all,
If I have a txt file in the format of:
dt 0.001000
tfinal 50.000000
A 6000
B 22.0
C 36.000000
D 9000.000000
E 5.700000
F 15.000000
batch_run from t = 0 to 50 in steps of 0.001 with dt = 0.001
........................................................................
........................................................................
........................................................................
........................................................................
........................................................................
........................................................................
where ..... above represents a (n x m) data matrix.
How would it be best to go about reading the values from the headers (dt, tfinal, A,..,F) and setting them as new variables in my workspace, ignoring the line "batch_run from t = 0 to 50...".
Then in addition to this separately reading just the data matrix, creating a new variable in my workspace where all the headers have been removed and only the matrix remains.
Many thanks.
0 件のコメント
採用された回答
Image Analyst
2018 年 7 月 8 日
Obviously there's no built-in reader for that format of a file so you'll have to write your own. Use fgetl() like this:
% Open the file.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
while ischar(textLine)
% Read the remaining lines of the file.
fprintf('%s\n', textLine);
% Read the next line.
textLine = fgetl(fileID);
% Now parse the line
if contains(textLine, 'dt ')
elseif contains(textLine, 'tfinal ', 'IgnoreCase, true)
etc.
end
% All done reading all lines, so close the file.
fclose(fileID);
Basically you need to recognize when you are on a certain type of line and parse that line appropriately
8 件のコメント
Image Analyst
2018 年 7 月 9 日
This seems to work with the attached text file I created.
% Open the file.
fullFileName = fullfile(pwd, 'text.txt');
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = strtrim(fgetl(fileID));
while ischar(textLine)
% Print this line to the command window.
fprintf('%s\n', textLine);
% Now parse the line
if contains(textLine(1:2), 'dt')
dt = str2double(textLine(4:end))
elseif contains(textLine(1:6), 'tfinal', 'IgnoreCase', true)
tfinal = str2double(textLine(8:end))
elseif contains(textLine(1:2), 'A ', 'IgnoreCase', true)
A = str2double(textLine(3:end))
elseif contains(textLine(1:2), 'B ', 'IgnoreCase', true)
B = str2double(textLine(3:end))
elseif contains(textLine(1:2), 'C ', 'IgnoreCase', true)
C = str2double(textLine(3:end))
elseif contains(textLine(1:2), 'D ', 'IgnoreCase', true)
D = str2double(textLine(3:end))
elseif contains(textLine(1:2), 'E ', 'IgnoreCase', true)
E = str2double(textLine(3:end))
elseif contains(textLine(1:2), 'F ', 'IgnoreCase', true)
F = str2double(textLine(3:end))
elseif contains(textLine, 'batch_run from', 'IgnoreCase', true)
% Now, after this batch_run line we're expecting 5 lines with 5 numbers per line.
m = zeros(5, 5); % Preallocate space for speed.
for row = 1 : 5
% Read the next line.
textLine = fgetl(fileID);
m(row, :) = sscanf(textLine, '%f');
end
end
% Read the next line.
textLine = fgetl(fileID);
end
% All done reading all lines, so close the file.
fclose(fileID);
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!