How can I import data with line gaps meaning new data set?

1 view (last 30 days)
I've got a large data set in two columns tab delimited that I need to import from a txt file. The single file contains multiple data sets separated by a line space and I need them to separate them, two 'columns' per data set. Each data set is of unknown length. How can I import this type of data?

Answers (1)

Star Strider
Star Strider on 15 Aug 2016
You can do that with textscan. I don’t have your data file so I can’t write exact code, but the idea is something like this:
fidi = fopen('YourTextFile.txt','rt');
k1 = 1;
while ~feof(fidi)
Data{k1} = textscan(fidi, '%f%f', 'CollectOutput',1, 'Delimiter','\t');
fseek(fidi, 0, 0);
k1 = k1 + 1;
end
fclose(fidi);
That’s worked for me when I’ve needed it, since textscan will politely stop when it encounters non-numeric data, in this example. The fseek call positions it at the line after the one that stopped it, in this instance the next line of numeric data. It then reads until it hits another blank line, and continues until it reaches the end of the file. Each section of your data will be in a separate cell in the ‘Data’ variable.
You’ll have to experiment with this to get it to work with your file. If we’re lucky, it’ll work without much modification beyond inserting the correct file name. If not, I’ll need your file to experiment with.

Categories

Find more on Data Import and Export 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!