Error with readtable function

I have a non-standard file format that I wish to read and run the following:
Y = readtable(fileID.name,'FileType','text');
where fileID is a structure. However, I get an error:
Error using readtable (line 197)
An error occurred while trying to determine whether "readData" is a function
name.
Note: readtable detected the following parameters:
'Delimiter', ',', 'HeaderLines', , 'ReadVariableNames', true, 'Format', ''
Why am I getting this error? What is readData? I can't find any information about it. Any ideas about how to get around this? The function readtable runs fine, without an error, on my laptop in MATLAB r2021b, but I only have access to the older 2018 version on the cluster that I'm using and don't have admin privileges. EDIT: I attach an example in the final comment here.

12 个评论

My guess is that fileID is a non-scalar structure, so fileID.name is expanding to multiple arguments.
@Walter Roberson fileID is actually an N x 1 structure, where N is the number of files. In fact, I loop over the number of files and analyze each file using Y = readtable(fileID(k).name,'FileType','text'). I didn't show that in my initial post because I was trying to make everything as simple as possible, but maybe I made things too simple. Do you know how to resolve the error that I'm getting? Strange that I don't get it running the same code in r2021b.
I do not appear to have R2018a.app installed at the moment, except possibly on one of my Windows virtual machines.
If not readtable, would there be a way to get around this using another function that is available for R2018a? My files have headers with a mix of strings and numbers followed by many numbers. I could attach a smaller file if you think that would help.
Attaching a sample file couldn't hurt.
Here you go. I am interested only in the ID's between 0 and 10, i.e., not the 9 rows of headers before that.
ITEM: TIMESTEP
0
ITEM: NUMBER OF ATOMS
10
ITEM: BOX BOUNDS pp pp pp
0 120
0 120
0 120
ITEM: ATOMS id type mol x y z bP
0 1 0 44 69 32 -1
1 2 0 44 68 31 -1
2 0 0 44 69 30 -1
3 0 0 44 70 30 -1
4 2 0 45 71 31 -1
5 0 0 46 71 32 -1
6 2 0 45 70 33 -1
7 2 0 44 71 33 -1
8 2 0 43 72 32 -1
9 5 0 43 73 32 -1
10 8 0 43 74 32 -1
ITEM: TIMESTEP
25000000
ITEM: NUMBER OF ATOMS
10
ITEM: BOX BOUNDS pp pp pp
0 120
0 120
0 120
ITEM: ATOMS id type mol x y z bP
0 1 0 50 68 52 -1
1 2 0 50 67 51 -1
2 0 0 49 68 50 -1
3 0 0 48 69 51 -1
4 2 0 48 68 51 -1
5 0 0 47 67 50 -1
6 2 0 48 66 49 -1
7 2 0 48 66 50 -1
8 2 0 48 65 51 -1
9 5 0 47 66 51 -1
10 8 0 47 65 51 -1
ITEM: TIMESTEP
50000000
ITEM: NUMBER OF ATOMS
10
ITEM: BOX BOUNDS pp pp pp
0 120
0 120
0 120
ITEM: ATOMS id type mol x y z bP
0 1 0 50 68 52 -1
1 2 0 50 67 51 -1
2 0 0 49 68 50 -1
3 0 0 48 69 51 -1
4 2 0 48 68 51 -1
5 0 0 47 67 50 -1
6 2 0 48 66 49 -1
7 2 0 48 66 50 -1
8 2 0 48 65 51 -1
9 5 0 47 66 51 -1
10 8 0 47 65 51 -1
I should add, because of the non-standard file format I got an error message when I tried to upload the actual file.
One option might be to use the zip function to zip the file and the upload it instead of the original. That usually works unless the file is simply too large and the size limit prevents it.
@Star Strider Here's the simple example (test data).
Which items of the file do you want to read an keep? In what format?
@per isakson I want a huge array of all of the numbers in my example from 0 to 10 for all steps.

请先登录,再进行评论。

 采纳的回答

I assume that the entire text file and the result fits in your RAM.
"huge" means different things to different people. This functions reads and parses a 20K line file in 0.1 sec.
I think you can improve performance significantly by replacing for() by parfor().
%%
A = cssm_( 'test_lammpstrj.txt' );
size(A)
ans = 1×3
11 7 3
A(:,:,3)
ans = 11×7
0 1 0 50 68 52 -1 1 2 0 50 67 51 -1 2 0 0 49 68 50 -1 3 0 0 48 69 51 -1 4 2 0 48 68 51 -1 5 0 0 47 67 50 -1 6 2 0 48 66 49 -1 7 2 0 48 66 50 -1 8 2 0 48 65 51 -1 9 5 0 47 66 51 -1
%%
% tic
% A = cssm_( 'test_lammpstrj_large.txt' ); % 20,003 lines
% toc
% Elapsed time is 0.101381 seconds.
% Elapsed time is 0.101189 seconds.
%%
function num = cssm_( ffs )
%%
fid = fopen( ffs, 'rt' );
chr = reshape( fread( fid, '*char' ), 1,[] );
[~] = fclose( fid );
%%
cac = regexp( chr, 'ITEM: TIMESTEP\n', 'split' );
len = size( cac, 2 );
num = nan( 11, 7, len-1 ); % cac{1} is empty
for jj = 2 : len
ccc = textscan( cac{jj}, '%d%d%d%d%d%d%d', 'Headerlines',8, 'CollectOutput',true );
num(:,:,jj-1) = ccc{1};
end
end

更多回答(1 个)

I'm not sure what the error with readtable is about, but here's one way to read that text file (I've given it the extension .txt here but that doesn't matter) and return a cell array of tables:
fid = fopen('test.txt');
data = fread(fid,'*char').';
fclose(fid);
C = split(data,'ITEM: ');
C = split(C(startsWith(C,'ATOMS')),newline());
var_names = split(strtrim(C{1}));
C(:,[1 end]) = [];
T = cellfun(@(x)array2table(x,'VariableNames',var_names(2:end)), ...
num2cell(permute(str2double(split(C)),[2 3 1]),[1 2]), ...
'UniformOutput',false);
T{:}
ans = 11×7 table
id type mol x y z bP __ ____ ___ __ __ __ __ 0 1 0 44 69 32 -1 1 2 0 44 68 31 -1 2 0 0 44 69 30 -1 3 0 0 44 70 30 -1 4 2 0 45 71 31 -1 5 0 0 46 71 32 -1 6 2 0 45 70 33 -1 7 2 0 44 71 33 -1 8 2 0 43 72 32 -1 9 5 0 43 73 32 -1 10 8 0 43 74 32 -1
ans = 11×7 table
id type mol x y z bP __ ____ ___ __ __ __ __ 0 1 0 50 68 52 -1 1 2 0 50 67 51 -1 2 0 0 49 68 50 -1 3 0 0 48 69 51 -1 4 2 0 48 68 51 -1 5 0 0 47 67 50 -1 6 2 0 48 66 49 -1 7 2 0 48 66 50 -1 8 2 0 48 65 51 -1 9 5 0 47 66 51 -1 10 8 0 47 65 51 -1
ans = 11×7 table
id type mol x y z bP __ ____ ___ __ __ __ __ 0 1 0 50 68 52 -1 1 2 0 50 67 51 -1 2 0 0 49 68 50 -1 3 0 0 48 69 51 -1 4 2 0 48 68 51 -1 5 0 0 47 67 50 -1 6 2 0 48 66 49 -1 7 2 0 48 66 50 -1 8 2 0 48 65 51 -1 9 5 0 47 66 51 -1 10 8 0 47 65 51 -1
Or if you want a 3D numeric array instead:
fid = fopen('test.txt');
data = fread(fid,'*char').';
fclose(fid);
C = split(data,'ITEM: ');
C = split(C(startsWith(C,'ATOMS')),newline());
C(:,[1 end]) = [];
M = permute(str2double(split(C)),[2 3 1]);
disp(M);
(:,:,1) = 0 1 0 44 69 32 -1 1 2 0 44 68 31 -1 2 0 0 44 69 30 -1 3 0 0 44 70 30 -1 4 2 0 45 71 31 -1 5 0 0 46 71 32 -1 6 2 0 45 70 33 -1 7 2 0 44 71 33 -1 8 2 0 43 72 32 -1 9 5 0 43 73 32 -1 10 8 0 43 74 32 -1 (:,:,2) = 0 1 0 50 68 52 -1 1 2 0 50 67 51 -1 2 0 0 49 68 50 -1 3 0 0 48 69 51 -1 4 2 0 48 68 51 -1 5 0 0 47 67 50 -1 6 2 0 48 66 49 -1 7 2 0 48 66 50 -1 8 2 0 48 65 51 -1 9 5 0 47 66 51 -1 10 8 0 47 65 51 -1 (:,:,3) = 0 1 0 50 68 52 -1 1 2 0 50 67 51 -1 2 0 0 49 68 50 -1 3 0 0 48 69 51 -1 4 2 0 48 68 51 -1 5 0 0 47 67 50 -1 6 2 0 48 66 49 -1 7 2 0 48 66 50 -1 8 2 0 48 65 51 -1 9 5 0 47 66 51 -1 10 8 0 47 65 51 -1

4 个评论

@_ str2double is too slow for me to use. Runs on the order of minutes for the full system, if not longer. Would there be any other options here? I want to have the data as a huge array with all the numbers rather than a multidimensional one as you give above.
Here are some things you might try, and see which is fastest with your file.
fid = fopen('test.txt');
data = fread(fid,'*char').';
fclose(fid);
C = split(data,'ITEM: ');
C = split(C(startsWith(C,'ATOMS')),newline());
C(:,[1 end]) = [];
% the method in my answer:
tic
M = permute(str2double(split(C)),[2 3 1]);
toc
Elapsed time is 0.006691 seconds.
disp(M);
(:,:,1) = 0 1 0 44 69 32 -1 1 2 0 44 68 31 -1 2 0 0 44 69 30 -1 3 0 0 44 70 30 -1 4 2 0 45 71 31 -1 5 0 0 46 71 32 -1 6 2 0 45 70 33 -1 7 2 0 44 71 33 -1 8 2 0 43 72 32 -1 9 5 0 43 73 32 -1 10 8 0 43 74 32 -1 (:,:,2) = 0 1 0 50 68 52 -1 1 2 0 50 67 51 -1 2 0 0 49 68 50 -1 3 0 0 48 69 51 -1 4 2 0 48 68 51 -1 5 0 0 47 67 50 -1 6 2 0 48 66 49 -1 7 2 0 48 66 50 -1 8 2 0 48 65 51 -1 9 5 0 47 66 51 -1 10 8 0 47 65 51 -1 (:,:,3) = 0 1 0 50 68 52 -1 1 2 0 50 67 51 -1 2 0 0 49 68 50 -1 3 0 0 48 69 51 -1 4 2 0 48 68 51 -1 5 0 0 47 67 50 -1 6 2 0 48 66 49 -1 7 2 0 48 66 50 -1 8 2 0 48 65 51 -1 9 5 0 47 66 51 -1 10 8 0 47 65 51 -1
% another possibility, gives a 2D matrix:
tic
C_new = cellfun(@(x)sscanf(x,'%f'),C.','UniformOutput',false);
M = [C_new{:}].';
toc
Elapsed time is 0.004438 seconds.
disp(M);
0 1 0 44 69 32 -1 1 2 0 44 68 31 -1 2 0 0 44 69 30 -1 3 0 0 44 70 30 -1 4 2 0 45 71 31 -1 5 0 0 46 71 32 -1 6 2 0 45 70 33 -1 7 2 0 44 71 33 -1 8 2 0 43 72 32 -1 9 5 0 43 73 32 -1 10 8 0 43 74 32 -1 0 1 0 50 68 52 -1 1 2 0 50 67 51 -1 2 0 0 49 68 50 -1 3 0 0 48 69 51 -1 4 2 0 48 68 51 -1 5 0 0 47 67 50 -1 6 2 0 48 66 49 -1 7 2 0 48 66 50 -1 8 2 0 48 65 51 -1 9 5 0 47 66 51 -1 10 8 0 47 65 51 -1 0 1 0 50 68 52 -1 1 2 0 50 67 51 -1 2 0 0 49 68 50 -1 3 0 0 48 69 51 -1 4 2 0 48 68 51 -1 5 0 0 47 67 50 -1 6 2 0 48 66 49 -1 7 2 0 48 66 50 -1 8 2 0 48 65 51 -1 9 5 0 47 66 51 -1 10 8 0 47 65 51 -1
% another possibility, gives a 2D matrix:
tic
C_new = cellfun(@str2num,C.','UniformOutput',false);
M = vertcat(C_new{:});
toc
Elapsed time is 0.006444 seconds.
disp(M);
0 1 0 44 69 32 -1 1 2 0 44 68 31 -1 2 0 0 44 69 30 -1 3 0 0 44 70 30 -1 4 2 0 45 71 31 -1 5 0 0 46 71 32 -1 6 2 0 45 70 33 -1 7 2 0 44 71 33 -1 8 2 0 43 72 32 -1 9 5 0 43 73 32 -1 10 8 0 43 74 32 -1 0 1 0 50 68 52 -1 1 2 0 50 67 51 -1 2 0 0 49 68 50 -1 3 0 0 48 69 51 -1 4 2 0 48 68 51 -1 5 0 0 47 67 50 -1 6 2 0 48 66 49 -1 7 2 0 48 66 50 -1 8 2 0 48 65 51 -1 9 5 0 47 66 51 -1 10 8 0 47 65 51 -1 0 1 0 50 68 52 -1 1 2 0 50 67 51 -1 2 0 0 49 68 50 -1 3 0 0 48 69 51 -1 4 2 0 48 68 51 -1 5 0 0 47 67 50 -1 6 2 0 48 66 49 -1 7 2 0 48 66 50 -1 8 2 0 48 65 51 -1 9 5 0 47 66 51 -1 10 8 0 47 65 51 -1
These all take several minutes and then I need to terminate them. My full data set is 600 x 30000, and each cell has 7 entries as you see above. A real shame readtable won't work for some reason. On my laptop, readtable takes a few seconds on the full data set, but on the cluster, I get the error as I mention above.
Thank you very much.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by