How to read a value at the end of char from a text file?

4 次查看(过去 30 天)
Hi; I have a text file that contains characters and values. I need some values at the end of the character. Example ;
Number of windows=16
I need to extract only the value '16' at the end of the "Number of windows="
You can find the file attached
  1 个评论
Cris LaPierre
Cris LaPierre 2018-12-26
Starting on row 10, your data is just numeric. Are you trying to read the data in the headerlines? Which ones are you wanting to read? All of them? Is the text always the same? Is the data you are wanting to capture always numeric?

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2018-12-28
编辑:Jan 2018-12-28
Are the files "small" (< 1kB)? Then:
C = strsplit(fileread(FileName), '\n');
Key = 'Number of windows'
mKey = strncmpi(C, ['# ', Key], length(Key) + 2);
Line = C{m};
mEq = strfind(Line, '=');
Value = sscanf(Line(mEq+1:end), '%g')
For larger files importing the complete file is a waste of time. Then:
Key = '# Number of windows'
fid = fopen(FileName, 'r');
if fid < 0
error('File not found: %s', FileName);
end
ready = false;
while ~ready
Line = fgetl(fid);
if strncmpi(Line, Key, length(Key))
mEq = strfind(Line, '=');
Value = sscanf(Line(mEq+1:end), '%g');
ready = true;
end
end
fclose(fid);

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by