An alternative is to use IMPORTDATA to read the entire file into a cell array of strings (with one line per cell), then use STRMATCH to find the cell that contains the string 'I_NEED_THIS_STRING'
, then use SSCANF to extract the 3 values from that cell:
>> data = importdata('mostly_useless_text.txt','\n'); %# Load the data
>> index = strmatch('I_NEED_THIS_STRING',data); %# Find the index of the cell
%# containing the string
>> values = sscanf(data{index},'I_NEED_THIS_STRING = %f %f %f') %# Read values
values =
1.0e+003 *
1.2345
6.7890
1.2345
If the file potentially has a lot of useless text before or after the line you are interested in, then you may use up a lot of memory in MATLAB by loading it all into a variable. You can avoid this by loading and parsing one line at a time using a loop and the function FGETS:
fid = fopen('mostly_useless_text.txt','r'); %# Open the file
newLine = fgets(fid); %# Get the first line
while newLine ~= -1 %# While EOF hasn't been reached
if strmatch('I_NEED_THIS_STRING',newLine) %# Test for a match
values = sscanf(newLine,'I_NEED_THIS_STRING = %f %f %f'); %# Read values
break %# Exit the loop
end
newLine = fgets(fid); %# Get the next line
end
fclose(fid); %# Close the file