how to read a really large lines of data(ascii file format) to a table

조회 수: 29 (최근 30일)
i have a really large ascii file that contains about 23,46,503 number of lines. i need to read them to a table with data types of each column as only text and number. i tried to import them using import data tool but it is taking forever to scan and parse the data (im using matlab 2019a). can anyone suggest me a faster approach to read the file to a table please. i even tried codes such as readtable(), dlmread(), fopen, fscanf etc. nothing works and it is throwing error.
it would be of great help if anyone helps
  댓글 수: 6
Allen
Allen 2020년 1월 25일
Does the start of the data in your .txt file contain any header lines or does it explicitly contain data in the format you have provided? Also, is this data tab-delimited?
Padmamalini  T H
Padmamalini T H 2020년 4월 6일
It has got headers. The datas are space seperated

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 1월 25일
filename = 'YourFile.txt':
[fid, msg] = fopen(filename);
if fid < 0
error('Failed to open file "%s" because "%s"', filename, msg);
end
%0.2120 1 CF00203x Rx d 8 00 00 00 FF FF 00 00 FF
fmt = ['%f%f%s%s', repmat('%x', 1, 10)];
datacell = textscan(fid, fmt, 'CollectOutput', true);
fclose(fid);
numeric_vals = [datacell{1}, datacell(3})];
text_vals = datacell{2};
  댓글 수: 5
Kavya Vuriti
Kavya Vuriti 2020년 4월 3일
You can try using importdata function to obtain structure containing data field and then use table function to convert it into table. As the file contains 23,46,503 number of lines, it takes around 1.66 seconds for importing.
Walter Roberson
Walter Roberson 2020년 4월 7일
i wanted the ascii file in a table in the workspace.
And what format is the table to be in?
You can use
array2table(numeric_vals)
and you can add in variables from text_vals{:,1} and text_vals{:,2} if you want a table() object.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by