Read specific columns of a text file
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a text file that contains 191 columns and over 64000 rows. Since this is a big file I only want to extract all the rows for the last 5 columns. How would I do this?
0 comentarios
Respuestas (2)
Robert
el 29 de Mzo. de 2016
I recommend you check out datastore.
docsearch Read and Analyze Large Tabular Text File
You can use its SelectedVariableNames property to tell it to read only your desired columns.
You could also use fscanf. An asterisk in the format specifier tells fscanf to skip that field.
0 comentarios
Orion
el 29 de Mzo. de 2016
Editada: Orion
el 29 de Mzo. de 2016
Assume you have this txt file that contains a matrix with 8 columns.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173862/image.jpeg)
you can read the 5 last columns like this :
% filename
filename = 'mytxtfile.txt';
% pattern to analyse the txt file
formatSpec = [repmat('%*d',1,8-5+1) repmat('%d',1,5) '%[^\n\r]'];
delimiter = ' ';
% read file
fid = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec,'Delimiter', delimiter);
fclose(fileID);
% convert into matrix
MyLast5Columns = cell2mat(dataArray(1:5));
You just have to adapt this to your file (number of columns, type of numbers (float, integer,..)).
0 comentarios
Ver también
Categorías
Más información sobre Large Files and Big Data 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!