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 extract data out of a text file

Subject: How to extract data out of a text file

From: Diego Zegarra

Date: 13 Oct, 2008 19:49:01

Message: 1 of 3

Hey guys,
I have .txt files that have the following structure and they all are different.

12
120

81 79 85 97 91 75 60 98 79 56 91 82
...
..
.
74 74 64 86 90 59 98 74 53 56 81 58
50 90 76 68 53 57 60 73 72 62 99 55

69 74 75 62 76 55 67 55 78 96 69 79 69 97 53 59 72 ... 56
...
..
.
89 78 91 72 59 52 92 96 95 95 74 98 99 68 55 69 66 ... 76

So let me explain what this means. First the 12 and 120 means 12 machines (m) and 120 jobs (n). I want to be able to save this as a variable.

Then the next block shows a number of jobs times number of machines matrix which I also want to save as a different variable.

After that there are m blocks of data (12 in this case) with a matrix of nxn. I want to be able to save each of these blocks in a variable each.

All my files will have a different number of machines and jobs so I want MATLAB to be able to read this automatically.

Thank you so much for ANY help that is given! I appreciate your time responding to this!

Subject: How to extract data out of a text file

From: Andres

Date: 13 Oct, 2008 20:17:01

Message: 2 of 3

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gd08nd$dsr$1@fred.mathworks.com>...
> Hey guys,
> I have .txt files that have the following structure and they all are different.
>
> 12
> 120
>
> 81 79 85 97 91 75 60 98 79 56 91 82
> ...
> ..
> .
> 74 74 64 86 90 59 98 74 53 56 81 58
> 50 90 76 68 53 57 60 73 72 62 99 55
>
> 69 74 75 62 76 55 67 55 78 96 69 79 69 97 53 59 72 ... 56
> ...
> ..
> .
> 89 78 91 72 59 52 92 96 95 95 74 98 99 68 55 69 66 ... 76
>
> So let me explain what this means. First the 12 and 120 means 12 machines (m) and 120 jobs (n). I want to be able to save this as a variable.
>
> Then the next block shows a number of jobs times number of machines matrix which I also want to save as a different variable.
>
> After that there are m blocks of data (12 in this case) with a matrix of nxn. I want to be able to save each of these blocks in a variable each.
>
> All my files will have a different number of machines and jobs so I want MATLAB to be able to read this automatically.

Maybe txt2mat (file exchange) can help you with this; you could simply type
A = txt2mat('C:\filename.txt');
to get in all the data into one larger matrix (padded with NaNs where no data is available) and then examine this matrix to find out the indices of interest.
Hth,
Andres

Subject: How to extract data out of a text file

From: Andres

Date: 14 Oct, 2008 07:01:03

Message: 3 of 3

"Andres " <rantore@werb.deNoRs> wrote in message <gd0abt$1ro$1@fred.mathworks.com>...
> [..]
> A = txt2mat('C:\filename.txt');
> [..]

A small correction: rather tell txt2mat the number of header lines (zero, I guess), to ensure that the first two lines with the single numbers are imported to A as well:
A = txt2mat('C:\filename.txt',0);

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