Clear Filters
Clear Filters

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

4 views (last 30 days)
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 Comment
Cris LaPierre
Cris LaPierre on 26 Dec 2018
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?

Sign in to comment.

Answers (1)

Jan
Jan on 28 Dec 2018
Edited: Jan on 28 Dec 2018
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);

Community Treasure Hunt

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

Start Hunting!