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

Shawntae Harris
Shawntae Harris 2020년 7월 22일
편집: per isakson 2020년 7월 22일
Here is the text file
Shawntae Harris
Shawntae Harris 2020년 7월 22일
I need the tempreture of 2 1,3,4 are not needed
Walter Roberson
Walter Roberson 2020년 7월 22일
MATLAB does not permit us to copy and paste pictures of code or text and have them processed as text.
Walter Roberson
Walter Roberson 2020년 7월 22일
So the lines for '2 FAIL OPEN_CIRCUIT' should be located? And is it the 22.8125 that you need, or the 100 0.00 on the next line that you need?
Shawntae Harris
Shawntae Harris 2020년 7월 22일
the tempretures need to be located so yes the 22.8125 but only the ones associated with the "2 FAIL-"
Shawntae Harris
Shawntae Harris 2020년 7월 22일
also this is just a picture of the text file the text file is an actual .txt file

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2020년 7월 22일

0 개 추천

filename = 'HypothesisTest2.txt';
S = fileread(filename);
temperatures = str2double( regexp(S, '^(?<=2 FAIL OPEN_CIRCUIT Int ')[\d.]+', 'match') );

댓글 수: 4

Shawntae Harris
Shawntae Harris 2020년 7월 22일
I am having a problem with the bracket before the backslash by d,+'
here is the error
temperatures = str2double( regexp(S, '^(?<=2 FAIL OPEN_CIRCUIT Int ')[\d.]+', 'match') );
Error: Unbalanced or unexpected parenthesis or
bracket.
temperatures = str2double( regexp(S, '^(?<=2 FAIL OPEN_CIRCUIT Int )[\d.]+', 'match') );
Shawntae Harris
Shawntae Harris 2020년 7월 22일
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.
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') );

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mohammad Sami
Mohammad Sami 2020년 7월 22일
편집: Mohammad Sami 2020년 7월 22일

0 개 추천

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

Shawntae Harris
Shawntae Harris 2020년 7월 22일
I would need it to be a for loop as the text file can vary for sizes

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by