MATLAB Answers

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

36 views (last 30 days)
Padmamalini  T H
Padmamalini T H on 23 Jan 2020 at 11:20
Commented: Padmamalini T H on 30 Jan 2020 at 11:06
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


Show 2 older comments
Mohammad Sami
Mohammad Sami on 25 Jan 2020 at 14:30
Yes with textscan, you need to specify the format spec. for the column that is mixed, import it as char.
Padmamalini  T H
Padmamalini T H on 25 Jan 2020 at 18:56
Can i have the code to read an ascii file. Because When i give .asc it is showing error, when i give .txt or .dat(since the data is in a notepad) it shows error
Allen on 25 Jan 2020 at 22:18
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?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 25 Jan 2020 at 22:53
filename = 'YourFile.txt':
[fid, msg] = fopen(filename);
if fid < 0
error('Failed to open file "%s" because "%s"', filename, msg);
%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);
numeric_vals = [datacell{1}, datacell(3})];
text_vals = datacell{2};


Padmamalini  T H
Padmamalini T H on 30 Jan 2020 at 5:56
the file is an asc file sir. when i give the filename with .txt extension it is showing error no such file or directory found. and matlab doesnt allow .asc extension too. im new to matlab and i dont know much about it
Walter Roberson
Walter Roberson on 30 Jan 2020 at 6:04
filename = 'YourFile.asc':
MATLAB is happy to use fopen() to open a .asc file, as long as the file exists in the named location, or somewhere along the MATLAB path, and you have permission to access the file.
If the file is not in your current directory where you run the code, then you should assign the fully qualified name to filename such as
filename = 'C:\Users\TPad\Documents\MATLAB\Project13b\MyBigFile.asc';
Padmamalini  T H
Padmamalini T H on 30 Jan 2020 at 11:06
thanks now i have called the asc file into the matlab environment. but i wanted the ascii file in a table in the workspace.

Sign in to comment.

Sign in to answer this question.


Translated by