Error with readtable function
11 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
per isakson
2022-4-2
编辑:per isakson
2022-4-2
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)
A(:,:,3)
%%
% 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
0 个评论
更多回答(1 个)
Voss
2022-4-2
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{:}
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!