Importing cnv file with header lines

I have a folder of cnv files that I need to import into matlab. Each file has about 130 header lines. I attempted to use the code
% Data = readtable('d941006.cnv','HeaderLines',130);
but received an error. I can only use import function if I remove the header lines from each file, but I have 171 files to upload and that would be too time consuming. I attached the link to the data as well. Any help would be much appreciated data file

 Respuesta aceptada

per isakson
per isakson el 2 de Ag. de 2017
Editada: per isakson el 5 de Ag. de 2017
Given
  • the entire file fits in a fraction of the memory
  • *END* is the last line before the numerical part of the file. (This is the only occurrence of *END*.)
  • all files have 29 columns of numerical data
then one way is
str = fileread( 'd94i006.txt' );
str = regexp( str, '(?<=\*END\*\s+).+$', 'match' );
cac = textscan( str{1}, repmat('%f',[1,29]), 'CollectOutput',true );
num = cac{1};
result
>> whos num
Name Size Bytes Class Attributes
num 339x29 78648 double
Note: "has about 130 header lines." makes it safer to use the line *END*.
In response to comment "loop [...] every file from the folder?"
Try
>> cac = cssm( 'c:\your_folder\with_data\' );
where in one file
function cac = cssm( folderspec )
sas = dir( fullfile( folderspec, '*.txt' ) );
len = length( sas );
cac = cell( 1, len );
for jj = 1 : len
cac{jj} = cssm_( fullfile( folderspec, sas(jj).name ) );
end
end
function num = cssm_( filespec )
str = fileread( filespec );
str = regexp( str, '(?<=\*END\*\s+).+$', 'match' );
cac = textscan( str{1}, repmat('%f',[1,29]), 'CollectOutput',true );
num = cac{1};
end
As is; Not tested Needed: better names and some comments. There is a magic number, 29, in the code.

3 comentarios

andrea molina
andrea molina el 4 de Ag. de 2017
that worked thank you so much!
andrea molina
andrea molina el 4 de Ag. de 2017
would there be a way to add that to a loop if I wanted to import every file from the folder?
per isakson
per isakson el 5 de Ag. de 2017
See added code

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Import and Analysis en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 2 de Ag. de 2017

Editada:

el 5 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by