Search code examples
matlabreadfileformat-specifierstextscan

Textscan Matlab ; Doesn't read the format


I have a file in the following format:

**400**,**100**::400,descendsFrom,**76**::0
**400**,**119**::400,descendsFrom,**35**::0
**400**,**4**::400,descendsFrom,**45**::0
...
...

Now I need to read, the part only in the bold. I've written the following formatspec:

formatspec = '%d,%d::%*d,%*s,%d::%*d\n';
data = textscan(fileID, formatspec);

It doesn't seem to work. Can someone tell me what's wrong? I also need to know how to 'not use' delimiter, and how to proceed if I want to express the exact way my file is written in, for example in the case above.


Solution

  • EDITED

    A possible problem is with the %s part of the formatspec variable. Because %s is an arbitrary string therefore the descendsFrom,76::0 part of the line is ordered to this string. So with the formatspec '%d,%d::%d,%s,%d::%d\n' you will get the following cells form the first line:

    400 100 400 'descendsFrom,76::0'

    To solve this problem you have two possibilities:

    formatspec = %d,%d::%d,descendsFrom,%d::%d\n
    

    OR

    formatspec = %d,%d::%d,%12s,%d::%d\n
    

    In the first case the 'descendForm' string has to be contained by each row (as in your example). In the second case the string can be changed but its length must be 12.