Effacer les filtres
Effacer les filtres

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

4 vues (au cours des 30 derniers jours)
serhat tekebas
serhat tekebas le 5 Oct 2018
Modifié(e) : Jan le 28 Déc 2018
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 commentaire
Cris LaPierre
Cris LaPierre le 26 Déc 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?

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 28 Déc 2018
Modifié(e) : Jan le 28 Déc 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!

Translated by