Read specific column from .txt file with unkown format

1 visualización (últimos 30 días)
Ilhem
Ilhem el 12 de Oct. de 2017
Comentada: Cedric el 13 de Oct. de 2017
Hello all, I am a beginner in matlab. I’m trying to import specific rows from a .txt file where the file format is unknown. Actually, the existing solutions allow me only to get rows instead of columns. For example, I want to read from the attached file, the column n=2 that includes (V1 D3 6.52 4.91 3.00 2.05 0.69 NAN NAN NAN) and to store the result into an array.
  1 comentario
Cedric
Cedric el 12 de Oct. de 2017
There must be a few things that you know about the format. Do you need the first rows or just the numeric data? Is the first row always containing those V + a column index or not?

Iniciar sesión para comentar.

Respuesta aceptada

Cedric
Cedric el 12 de Oct. de 2017
Editada: Cedric el 12 de Oct. de 2017
content = fileread( 'Info.txt' ) ;
nCols = numel( strsplit( regexp( content, '[^\r\n]+', 'match', 'once' ), ' ')) ;
data = reshape( regexp(content, '\s+', 'split'), nCols, [] ).' ;
header = data(1:2,:) ;
data = str2double( data(3:end,:) ) ;
and then you can pick any column in both header and data. Or you can use the simpler:
data = importdata( 'Info.txt' ) ;
and see which field provides you with what you need.
  5 comentarios
Ilhem
Ilhem el 13 de Oct. de 2017
That works well. Thank you!
Cedric
Cedric el 13 de Oct. de 2017
My pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by