Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to import & separate two sets of from a single .txt file?

Subject: How to import & separate two sets of from a single .txt file?

From: D

Date: 29 May, 2010 23:55:10

Message: 1 of 2

Hello,

I am having difficulty figuring out in Matlab how to import two sets of data located within a single .txt file:
- Each data set has its own row x col dimensions and headers
- The second data set is seen a few tabs beneath the last row of the first data set
- txt file is tab delimited.

So, for example, a .txt file looks something like this:
data set #1:
row(1): header(1) header(2) header(3) ... header(m+10)
. data(1) . . .
. . . . .
row(n): data(n) . . .

data set #2:
row(n+5): header(1) header(2) header(3) ... header(m+6)
. data(n+6) . . .
. . . . .
row(...): data(...) . . .

Since I have many txt files to import, I would like to import directly into Matlab, and would like to avoid txt > excel > matlab. I have tried importdata, and it works great up to row(n+5), but I can't it get it start at row(n+5) to end.

Thank you for reading, and I appreciate any advice and direction.

- Dale

Subject: How to import & separate two sets of from a single .txt file?

From: TideMan

Date: 30 May, 2010 02:28:16

Message: 2 of 2

On May 30, 11:55 am, "D " <dale....@gatech.edu> wrote:
> Hello,
>
> I am having difficulty figuring out in Matlab how to import two sets of data located within a single .txt file:
> - Each data set has its own row x col dimensions and headers
> - The second data set is seen a few tabs beneath the last row of the first data set
> - txt file is tab delimited.
>
> So, for example, a .txt file looks something like this:
> data set #1:
> row(1):  header(1)  header(2)  header(3) ... header(m+10)
> .            data(1)               .              .                  .
> .                  .                  .              .                  .
> row(n):   data(n)               .              .                  .
>
> data set #2:
> row(n+5):  header(1)  header(2)  header(3) ... header(m+6)
> .               data(n+6)         .              .                  .
> .                   .                  .              .                  .
> row(...):    data(...)            .              .                  .
>
> Since I have many txt files to import, I would like to import directly into Matlab, and would like to avoid txt > excel > matlab.  I have tried importdata, and it works great up to row(n+5), but I can't it get it start at row(n+5) to end.
>
> Thank you for reading, and I appreciate any advice and direction.
>
> - Dale

Assuming n is not too large (a few thousand, say), the way I'd do it
is to read the entire file into Matlab using fscanf:
a=fscanf(fid,'%c');
a is now a long string with LF i.e. char(10) marking the end of each
line.
You can use the Matlab string finding functions to locate the end of
the first block and the start of the next, then you can use sscanf to
transfer the data into matrices.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us