Text reader to numbers

1 visualización (últimos 30 días)
Shawntae Harris
Shawntae Harris el 22 de Jul. de 2020
Comentada: Walter Roberson el 22 de Jul. de 2020
I have to make a program that goes through a text file and extracts numerical values, I am having a hard time extracting those numbers, here is what I have so far. The text file also has a lot if letters associated with it to
  6 comentarios
Shawntae Harris
Shawntae Harris el 22 de Jul. de 2020
the tempretures need to be located so yes the 22.8125 but only the ones associated with the "2 FAIL-"
Shawntae Harris
Shawntae Harris el 22 de Jul. de 2020
also this is just a picture of the text file the text file is an actual .txt file

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de Jul. de 2020
filename = 'HypothesisTest2.txt';
S = fileread(filename);
temperatures = str2double( regexp(S, '^(?<=2 FAIL OPEN_CIRCUIT Int ')[\d.]+', 'match') );
  4 comentarios
Shawntae Harris
Shawntae Harris el 22 de Jul. de 2020
I do have a problem as it returns an array of NaN. I think this is due to the fact that there are cell array of cell arrays.
Walter Roberson
Walter Roberson el 22 de Jul. de 2020
If you were to post an actual file instead of an image of a file, I could test...
Oh, I do see one problem:
temperatures = str2double( regexp(S, '^(?<=2 FAIL OPEN_CIRCUIT Int )[\d.]+', 'match', 'lineanchors') );

Iniciar sesión para comentar.

Más respuestas (1)

Mohammad Sami
Mohammad Sami el 22 de Jul. de 2020
Editada: Mohammad Sami el 22 de Jul. de 2020
Assuming the pattern shown in your picture. Ignore line 1, line 2& line 3 form a repeating pattern.
I would suggest read the entire file first. Then we split it by line. Then put the line following line 2 pattern together and same for line3.
file = 'filename.txt';
fid = fopen(file,'r');
rawtxt = fread(fid,'*char')';
rawtxt = splitlines(rawtxt);
line2s = rawtxt(2:2:end);
line3s = rawtxt(3:2:end);
line2txt = strjoin(line2s,newline);
line3txt = strjoin(line3s,newline);
out2 = textscan(line2txt,'%f FAIL OPEN_CIRCUIT Int %f degC');
out3 = textscan(line3txt,'%f %f W/m^2');
  1 comentario
Shawntae Harris
Shawntae Harris el 22 de Jul. de 2020
I would need it to be a for loop as the text file can vary for sizes

Iniciar sesión para comentar.

Categorías

Más información sobre Language Support en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by