1
votes

I want to read in a text file (using matlab) with data that is not in a convenient matlab matrix form. This is an example:

{926377200,926463600}

[(48, 13), (75, 147), (67, 13)]

{926463600,926550000}

[(67, 48)]

{926550000,926636400}

[]

{926636400,926722800}

[]
{926722800,926809200}
...

All I would like is a vector of all the numbers separated by commas. With them always being in pairs and the odd lines' numbers are of much greater magnitude each time, this can be differentiated by logic later.

I cannot figure out how to use textscan or the other methods. What makes this a bit tricky is that the matlab methods require a defined format for the strings separated by delimiters and here the even lines have non-restricted numbers of integer pairs.

2
can a string tokenizer ("strtok") not be used? How would the text file be fed to the tokenizer? thanks - Vass

2 Answers

2
votes

You can do this with textscan. You just need to specify the {} etc as whitespace.

For example, if you put your sample data into the file tmp.txt (in the current directory) and run the following:

fid = fopen('tmp.txt','r');
if fid > 0
   numbers = textscan(fid,'%f','whitespace','{,}[]() ');
   fclose(fid);
    numbers = numbers{:} 
end

you should see

numbers =

   926377200
   926463600
          48
          13
          75
         147
          67
          13
   926463600
   926550000
          67
          48
   926550000
   926636400
   926636400
   926722800
   926722800
   926809200
1
votes

Just iterate through each character. (use fscanf or fread or whatever). If the character is a number (use str2num) , store it as a number , if it is not a number, discard it and start storing a new number when you encounter the next number.