How to read a non uniform text file in Matlab

4 views (last 30 days)
Hello!
I have a non uniform text file which I want to read in MATLAB. The file may be found here:
Can I create a matrix where cells could be left blank while some of them would recognize characters and others numbers?
From this file I wish to keep every second value (first column)
For example from every b group like the one following
b2
5.00 0.00 0.00
5.00 0.00
I only wish to keep the respective 5.0 value in a single column matrix along with all the other respective values from b1, b3, b4, .... , b48.
Thank you so very much!
Giorgos

Accepted Answer

Thorsten
Thorsten on 17 Jan 2013
If you have to do it just for this file you can make your life easy by not reading the file at all:
b = 0:5:235;
  1 Comment
Giorgos Tassis
Giorgos Tassis on 17 Jan 2013
Thank you again Thorsten for your answer!
Unfortunately this not the only case I will come across. This is just a simple example of this non uniform file. So, I hate to make your life difficult together with mine but I really need this file pattern read in MATLAB and stored in a matrix (if possible of course).
I tried to do it with a for loop command like this (but didn't work obviously)
for i=1:1:3*48;
bor1 = fgets(fid);
bor2 = fgets(fid);
BOR(i) = sscanf(bor2,'%d');
bor3 = fgets(fid);
end;
I hope you can help me!
Giorgos

Sign in to comment.

More Answers (1)

Thorsten
Thorsten on 18 Jan 2013
Edited: Thorsten on 18 Jan 2013
Hi Giorgios, this file should do the job:
fid = fopen('file2.txt');
s = fgets(fid); % get line with a 'b'
i = 1;
while s ~= -1
d = fscanf(fid, '%f\n', 5); % get next 5 numbers
b(i) = d(1); % store first number in b
i = i + 1;
s = fgets(fid);
end
fclose(fid)
  2 Comments
Giorgos Tassis
Giorgos Tassis on 18 Jan 2013
Thanks again Thorsten!
I only have one last question if you'd be so kind to answer.
What if this is only a part of a bigger file and I wish to stop the "while" cycle at the end of the b groups? Let's say that after these b groups follows an integer, for example 50.
b48
5.00 0.00 0.00
5.00 0.00
50 ... etc
Any ideas?
Cheers!
Giorgos Tassis
Giorgos Tassis on 22 Jan 2013
Hello again!
Still in trouble about this particular part of the code. This is just a reminder.
Thanks a lot!

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!