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
Walter Roberson
2022년 4월 1일
My guess is that fileID is a non-scalar structure, so fileID.name is expanding to multiple arguments.
L'O.G.
2022년 4월 1일
Walter Roberson
2022년 4월 1일
I do not appear to have R2018a.app installed at the moment, except possibly on one of my Windows virtual machines.
L'O.G.
2022년 4월 1일
Voss
2022년 4월 1일
Attaching a sample file couldn't hurt.
L'O.G.
2022년 4월 1일
L'O.G.
2022년 4월 1일
Star Strider
2022년 4월 2일
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.
L'O.G.
2022년 4월 2일
per isakson
2022년 4월 2일
Which items of the file do you want to read an keep? In what format?
L'O.G.
2022년 4월 2일
채택된 답변
추가 답변 (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{:}
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);
댓글 수: 4
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
disp(M);
% another possibility, gives a 2D matrix:
tic
C_new = cellfun(@(x)sscanf(x,'%f'),C.','UniformOutput',false);
M = [C_new{:}].';
toc
disp(M);
% another possibility, gives a 2D matrix:
tic
C_new = cellfun(@str2num,C.','UniformOutput',false);
M = vertcat(C_new{:});
toc
disp(M);
L'O.G.
2022년 4월 2일
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!