How to import data into a dataset from a space-delimited ascii file.

22 views (last 30 days)
I have several large (10's and 100's of MByte) ascii files. First row contains variable names. Rest are data. All arranged in columns. Example file is attached. Variables are mix of floats, integers, binary and hex. If I use interactive Import Data and choose 'dataset' with everything else set to defaults, this always works. Would like to do the same thing, but by program. I like the dataset approach because of the dot subscripting and the import seems quick.
But, the closest I can get to success is below, but gives the following errors...
K>> ctlset = dataset('File',[name, '_CtlDat1.dat'],'VarNames',true,'Delimiter',' '); Error using dataset/readFile>tdfread (line 302) Requires the same number of delimiters on each line.
Error in dataset/readFile (line 163) raw = tdfread(file,delimiter,tdfreadHeaderLines,treatAsEmpty);
Error in dataset (line 347) a = readFile(a,fileArg,otherArgs);

Accepted Answer

Peter Perkins
Peter Perkins on 13 Oct 2015
Mark, I think you're going to need to use MultipleDelimsAsOne for this file. To do that, you're gonna need to pass in a format, something like [repmat('%f',1,23) '%s%s'] depending on what you want to do with the binary and the hex values. Except that at the end of the file on line 73496, you have a partial line that you'll need to fix (or maybe the attachment was somehow truncated).
If you're using R2013b or later, you might think about using table (and readtable) rather than dataset. I was able to get these data in using
t = readtable('5617B608_Blanco_09Oct15_TBEI_Enp1_CtlDat1.dat', ...
'Format',[repmat('%f',1,23) '%s%s'], ...
'Delimiter',' ','MultipleDelimsAsOne',true);
Hope this helps.
  2 Comments
Mark Fredette
Mark Fredette on 14 Oct 2015
thanks, Peter... I will stop banging my head against the wall and upgrade! (Had been using R2012.)
Appreciate very much your taking time to look at this in such detail.
Peter Perkins
Peter Perkins on 16 Oct 2015
You should be able to do something similar with dataset even in R2012a/b.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 13 Oct 2015
No data file was attached. Did you remember to click the "Attach file" button after you clicked the "Choose file" button?
Have you tried readtable() or dlmread()?
  1 Comment
Mark Fredette
Mark Fredette on 13 Oct 2015
Thanks for the instantaneous response! I thought I had, but let me try to attach again. In the meantime, will check out your suggestions.
mark

Sign in to comment.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!