Home Finding the format of arbitrary delimited text file in MATLAB
Reply: 1

Finding the format of arbitrary delimited text file in MATLAB

Steve Hatcher
Steve Hatcher Published in 2014-04-25 14:15:13Z

I have a file that looks like this in notepad++

I can easily see the spaces (being the orange dots), and tabs (being the orange arrows). I can also right click this in MATLAB and import it in a variety of ways. The problem is firstly the delimiters are not consistent. It seems to go TAB then some spaces to make sure the total field equals 6 characters...

The only way I understand reading a file in is if you already know how it is delimited. But in this case I would like to parse each line so MATLAB has some 'token' of what goes where eg:

Line1: Text Space Text Space Text Tab Space Space Text NEWLINE

(Notepad++ seems to know just fine so surely MATLAB can get this info too?).

Is this possible? Then it would be nice to use this information to save the imported data back out to a file with exactly the same formatting.

The data is below. For some reason copying this into notepad++ does not preserve its delimiting, you will need to add the tabs in yourself so it looks like the file in the screenshot.

Average Counts :      56.2
Time           :       120


Lisa Reply to 2014-04-25 15:37:15Z

If you use textscan, the default behaviour should probably suit your needs:

Within each row of data, the default field delimiter is white space. White space can be any combination of space (' '), backspace ('\b'), or tab ('\t') characters. If you do not specify a delimiter, textscan interprets repeated white-space characters as a single delimiter.

The output is a cell array, where each column is saved as a cell. So C{1} would contain the strings, C{2} the colons, and C{2} the values.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.353111 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO