Scaning a txt file

1 次查看(过去 30 天)
Hello!
I want to read specific data from several txt files (result from a AVL simulation ) and later plot that information into a table and a 3D graphic. The problem is that the information in the txt file is not "organized". I want to get the values of the Engine speed, the BMEP and BSFC. Attached is one of the 17 txt files. They are all the same, only the values change in function of the engine speed.

采纳的回答

Mathieu NOE
Mathieu NOE 2021-1-13
hello Carolina
this code will scan the file and search for the targeted variables
you can expand (add more variables) if you wish
it will retrieve associated numerical data , whatever the dimensions (width) of the data
hope it helps
Filename = 'Case_1.txt';
fid = fopen(Filename);
tline = fgetl(fid);
while ischar(tline)
if contains(tline,'Engine Speed :')
tmp = strtrim(regexp(tline, '\s+', 'split')); %split by spaces
dd = regexp(tmp,'-?[0-9]+.[0-9]+', 'match'); % extract numeric parts only inside the cell (in case of mix with text)
str_temp = strjoin( cat(2,dd{:}), ',') ; % Transform a cell array of cell arrays into string / delimiter = blank or comma
Engine_Speed = str2num(str_temp); % convert string to num
end
if contains(tline,'BMEP [bar]')
tmp = strtrim(regexp(tline, '\s+', 'split')); %split by spaces
dd = regexp(tmp,'-?[0-9]+.[0-9]+', 'match'); % extract numeric parts only inside the cell (in case of mix with text)
str_temp = strjoin( cat(2,dd{:}), ',') ; % Transform a cell array of cell arrays into string / delimiter = blank or comma
BMEP = str2num(str_temp); % convert string to num
end
if contains(tline,'BSFC [g/kWh]')
tmp = strtrim(regexp(tline, '\s+', 'split')); %split by spaces
dd = regexp(tmp,'-?[0-9]+.[0-9]+', 'match'); % extract numeric parts only inside the cell (in case of mix with text)
str_temp = strjoin( cat(2,dd{:}), ',') ; % Transform a cell array of cell arrays into string / delimiter = blank or comma
BSFC = str2num(str_temp); % convert string to num
end
tline = fgetl(fid);
end
fclose(fid);
  3 个评论
Mathieu NOE
Mathieu NOE 2021-2-4
Glad it helped !

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by