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!