Search code examples
matlabfile-iotextscan

How do I read in a series of numbers using "Textscan" in MATLAB if the file is mostly text?


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.