Error with readtable function
Mostra commenti meno recenti
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 Commenti
Walter Roberson
il 1 Apr 2022
My guess is that fileID is a non-scalar structure, so fileID.name is expanding to multiple arguments.
L'O.G.
il 1 Apr 2022
Walter Roberson
il 1 Apr 2022
I do not appear to have R2018a.app installed at the moment, except possibly on one of my Windows virtual machines.
L'O.G.
il 1 Apr 2022
Voss
il 1 Apr 2022
Attaching a sample file couldn't hurt.
L'O.G.
il 1 Apr 2022
L'O.G.
il 1 Apr 2022
Star Strider
il 2 Apr 2022
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.
il 2 Apr 2022
per isakson
il 2 Apr 2022
Which items of the file do you want to read an keep? In what format?
L'O.G.
il 2 Apr 2022
Risposta accettata
Più risposte (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 Commenti
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.
il 2 Apr 2022
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!