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:
PLEASE help to extract data of text file into variables

Subject: PLEASE help to extract data of text file into variables

From: Diego Zegarra

Date: 14 Oct, 2008 20:55:04

Message: 1 of 8

Guys please I need help reading a text file like this. I have posted a couple questions regarding this but I dont find a solution for this. I have been trying to use fscanf, textscan, textread, txt2mat, etc and I cannot get them to work.

All my files are different but have the same structure. In this case it is a 3 machines (m) 6 jobs (n) as seen in the first two lines. Then it comes a matrix of jobs x machines (6x3) and then m number of matrices of (nxn). So basically the file structure can be known from the first two lines. Every matrix is separated by a blank line. This is an example of a file,

3
6

55 54 61
58 60 90
56 85 95
78 59 74
92 83 65
67 54 68
 
52 92 92 69 68 53
90 84 63 91 59 71
77 80 71 94 65 61
54 54 60 86 54 99
64 64 98 65 59 70
87 59 61 61 96 65

87 73 55 58 67 55
84 97 51 62 98 89
90 54 63 68 68 65
52 77 81 74 85 54
95 56 96 71 57 92
70 58 59 95 66 78
 
71 58 81 69 86 76
68 94 53 85 63 72
52 68 67 58 57 92
64 55 87 69 55 89
89 79 94 94 64 80
78 63 90 58 82 59

65 83 95 77 91 84
95 53 96 82 84 53
79 50 98 72 80 66
91 53 95 95 62 83
76 91 59 87 94 63
79 63 61 64 82 68

58 78 62 83 93 95
60 73 94 76 57 93
98 86 59 97 60 89
51 89 66 73 98 55
61 86 73 77 58 57
65 78 84 64 51 73

76 79 57 69 85 93
76 72 55 73 65 51
98 71 60 54 79 75
71 58 63 77 66 61
61 77 91 71 60 62
59 93 94 91 85 92

Please help me to see how I can assign a variable to each matrix. I do not need you to write the whole code if you do not have the time or dont have it at hand, i just ask you to give a guide on how to do it! Thank you!

Sorry for posting a different thread but I did not get anywhere with the one before and it does not seem to be getting any more input!

Subject: PLEASE help to extract data of text file into variables

From: Diego Zegarra

Date: 15 Oct, 2008 04:39:01

Message: 2 of 8

PLEASE CAN ANYONE GIVE ME SOME HELP!

THANK YOU!

Subject: PLEASE help to extract data of text file into variables

From: NZTideMan

Date: 15 Oct, 2008 07:25:14

Message: 3 of 8

On Oct 15, 5:39=A0pm, "Diego Zegarra" <diego...@gmail.com> wrote:
> PLEASE CAN ANYONE GIVE ME SOME HELP!
>
> THANK YOU!

Please don't shout.

You say that you've tried all these routines, but you "don't find a
solution".
It's pretty difficult to help you if you don't tell us what went
wrong.
If it were me, I'd use fscanf. You say you've tried it, but it didn't
work.
Why not post the code you used. Then we could help you de-bug it.

Subject: PLEASE help to extract data of text file into variables

From: Andres

Date: 15 Oct, 2008 07:54:02

Message: 4 of 8

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gd30v8$2un$1@fred.mathworks.com>...
> Guys please I need help reading a text file like this. I have posted a couple questions regarding this but I dont find a solution for this. I have been trying to use fscanf, textscan, textread, txt2mat, etc and I cannot get them to work.


Hi Diego,
this is at least the third thread you have opened for the same problem - so it is hard to keep track of it. Remember there are people from different time zones who are not checking the newsgroup every few minutes.

Now here's some code example with txt2mat - it is uncommented, but hopefully quite understandable. Just note that I created a cell array with ST{1}..ST{n} instead of separate variables ST_1..ST_n, and I would recommend this to you, too. I think there are also FAQ pages about this topic.
Hth,
Andres


% get matrices A, PT and ST{i} from text file
data = txt2mat('c:\filename.txt',0);
A = data([1,2],1).';
nextRowIndex = 3;
PT = data(nextRowIndex+(1:A(2)), 1:A(1));

ST = cell(A(1),1);
for idx = 1:A(1)
    nextRowIndex = nextRowIndex + 1 + A(2);
    ST{idx} = data(nextRowIndex+(1:A(2)), 1:A(2));
end

Subject: PLEASE help to extract data of text file into variables

From: Andres

Date: 15 Oct, 2008 08:25:03

Message: 5 of 8

"Andres " <rantore@werb.deNoRs> wrote in message <gd47iq$ql7$1@fred.mathworks.com>...
> % get matrices A, PT and ST{i} from text file
> data = txt2mat('c:\filename.txt',0);
> A = data([1,2],1).';
> nextRowIndex = 3;
> PT = data(nextRowIndex+(1:A(2)), 1:A(1));
>
> ST = cell(A(1),1);
> for idx = 1:A(1)
> nextRowIndex = nextRowIndex + 1 + A(2);
> ST{idx} = data(nextRowIndex+(1:A(2)), 1:A(2));
> end


Sorry there was a typo (swapping the jobs/machines indices), so here's a correction with more meaningful variable names. Btw, use File->Set Path to make sure matlab finds the folder where you copied txt2mat. If there are further problems don't hesitate to ask - preferrably give us error messages or more details about it.


% get matrices A, PT and ST{i} from text file
data = txt2mat('c:\filename.txt',0);
numMachines = data(1,1);
numJobs = data(2,1);
A = [numMachines,numJobs];
nextRowIndex = 3;
PT = data(nextRowIndex+(1:numJobs), 1:numMachines);

ST = cell(numJobs,1);
for idx = 1:numJobs
    nextRowIndex = nextRowIndex + 1 + numJobs;
    ST{idx} = data(nextRowIndex+(1:numJobs), 1:numJobs);
end

Subject: PLEASE help to extract data of text file into variables

From: Andres

Date: 15 Oct, 2008 08:39:01

Message: 6 of 8

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gd30v8$2un$1@fred.mathworks.com>...
> All my files are different but have the same structure. In this case it is a 3 machines (m) 6 jobs (n) as seen in the first two lines. Then it comes a matrix of jobs x machines (6x3) and then m number of matrices of (nxn). So basically the file structure can be known from the first two lines. Every matrix is separated by a blank line.

:-/
Regarding my typo...
Your description does not apply to your example. In the example, there are 6 6x6 matrices (n=6) at the end, not 3 (m=3). The latter code is suitable for files as in the example; you may have to make obvious changes to it.

Subject: Extract data of text file into variables

From: Diego Zegarra

Date: 16 Oct, 2008 00:25:04

Message: 7 of 8

Hey Andres thank you so much! It worked perfectly. You were right actually the description was right the example was wrong but I figured it out!

I have a question about your code though, just to know for further stuff that I will have to do.

You use idx in there which i guess it stands for Index but it is not defined earlier. Could you please explain me how it works?

Also, this line, what does it do when you put the "nextRowIndex+(1:numJobs)" as the row

ST{idx} = data(nextRowIndex+(1:numJobs), 1:numJobs);

Thanks again and sorry for posting this thread over and over!

Subject: Extract data of text file into variables

From: Andres

Date: 16 Oct, 2008 12:03:01

Message: 8 of 8

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gd61kv$jib$1@fred.mathworks.com>...
> I have a question about your code though, just to know for further stuff that I will have to do.
>
> You use idx in there which i guess it stands for Index but it is not defined earlier. Could you please explain me how it works?
>
> Also, this line, what does it do when you put the "nextRowIndex+(1:numJobs)" as the row
>
> ST{idx} = data(nextRowIndex+(1:numJobs), 1:numJobs);
>
> Thanks again and sorry for posting this thread over and over!


idx is indeed an index (it counts the final square matrices), and it is defined within

for idx = 1:numJobs

running from 1,2, ... up to numJobs through each pass of the loop (you probably changed this to numMachines according your description and hopefully wrote ST = cell(numMachines ,1); before, too).

The nextRowIndex variable always indicates the number of the row in the large data matrix right after the rows that we have just extracted the values from (which is the line number of the empty line in your text file). So to read the following matrix values, we start at nextRowIndex+1, and as we want to read numJobs rows, we stop at row number nextRowIndex+numJobs. nextRowIndex+(1:numJobs) gives us all those row numbers together.
As we can access parts of a 2D-matrix via subscripts like this:
Matrix(RowNumbers,ColumnNumbers)

we write

data(nextRowIndex+(1:numJobs), 1:numJobs);

to extract those final numJobs x numJobs matrices. Btw, data(nextRowIndex+(1:numJobs),:) would have worked as well.

Look at the "Matrices and Magic Squares" and "Matrix Indexing" pages in the matlab documentation and type "doc for" and "doc colon" for more detailed information.

In general, if you are not sure about the code, try to run it command-by-command and look what's happening to the variables (for statements inside a loop, set the loop counter manually). Matlab's debugging features are very helpful here, cf. the "Running M-Files in the Editor" page.
Regards
Andres

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