How do I read in a series of numbers using “Textscan” in MATLAB if the file is mostly text?
Question
I have a text file that have a string of 3 numbers that I need to read into MATLAB.
For Example:
#######################
#
#
# Text Text Text
#
#
#######################
Blah blah blah = ####
Blah blah blah = ####
Blah blah blah = ####
Blah blah blah = ####
Blah blah blah = ####
Blah blah blah = ####
I_NEED_THIS_STRING = 1234.5 6789.0 1234.5 !Comment blah blah blah
I need to read in those 3 numbers into an array.
PLEASE HELP.
Thanks
Solution
If most of the file is irrelevant to your application, I suggest preprocessing with your favorite scripting language or command line tool to find the relevant lines and use textscan() on that.
e.g., from a shell prompt:
grep ^I_NEED_THIS_STRING infile > outfile
in matlab:
fid = fopen('outfile');
C = textscan(fid, 'I_NEED_THIS_STRING = %f %f %f')
fclose(fid)
See the textscan documentation for more details.
OTHER TIPS
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