How to read a value at the end of char from a text file?
2 views (last 30 days)
Show older comments
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
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?
Answers (1)
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);
0 Comments
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!