Locate and Extract a number out of a Text File
7 views (last 30 days)
Show older comments
Hi guys,
I have a text file, in which there is a section looking like this:
BLOCK SIZE USED ...................... 7
NUMBER OF DECOMPOSITIONS ............. 4
NUMBER OF ROOTS FOUND ................ 211
NUMBER OF SOLVES REQUIRED ............ 77
I need to write a code which extracts the "211" value from the line "Number of Roots".
I have written the following script, which locates the line in the text file but I am unable to extract the number "211". Does anyone have an idea?
Here is what I wrote: >> CODE
fid = fopen('BIO-PDR_Stowed_Modal.f06','r') ;
Parser_REV_line = fgetl(fid);
while ischar(Parser_REV_line) % While loop to locate the target data in the f06 file
Parser_REV_line = fgetl(fid);
if contains(Parser_REV_line,'NUMBER OF DECOMPOSITIONS')
break
end
end
Parser_REV_line = fgetl(fid) ; % Returns the next line of the specified file, removing the newline characters.
while ischar(Parser_REV_line) % While the returned lined is a character array...
Parser_REV_line = fgetl(fid); % Get next line
if ~isempty(str2num(Parser_REV_line)) % IF the line returned, converted from string to numeric matrix is not empty...
%%%% This is where I can't extract the number %%%
end
if contains(Parser_REV_line,'NUMBER OF SOLVES REQUIRED') % When Parser_PF_line is equal to "NUMBER OF SOLVES REQUIRED" (i.e. end of data), program ends
break
end
end
fclose(fid) ;
0 Comments
Answers (0)
See Also
Categories
Find more on Data Import and Export 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!