Borrar filtros
Borrar filtros

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

4 visualizaciones (últimos 30 días)
serhat tekebas
serhat tekebas el 5 de Oct. de 2018
Editada: Jan el 28 de Dic. de 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 comentario
Cris LaPierre
Cris LaPierre el 26 de Dic. de 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?

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 28 de Dic. de 2018
Editada: Jan el 28 de Dic. de 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);

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by