How to read a non uniform text file in Matlab

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

 Respuesta aceptada

Thorsten
Thorsten el 17 de En. de 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 comentario

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

Iniciar sesión para comentar.

Más respuestas (1)

Thorsten
Thorsten el 18 de En. de 2013
Editada: Thorsten el 18 de En. de 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 comentarios

Giorgos Tassis
Giorgos Tassis el 18 de En. de 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 el 22 de En. de 2013
Hello again!
Still in trouble about this particular part of the code. This is just a reminder.
Thanks a lot!

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by