Error while using function readtable()

I use function readtable to read a txt file, it works well before, but suddenly it don't works anymore and return the error:
>> a2=readtable('dyw_e_u.txt');
Error using readtable
inputs must be a string array, character vector, or cell array of character vectors.
>> a2=readtable("dyw_e_u.txt");
Error using readtable
inputs must be a string array, character vector, or cell array of character vectors.
I think what I input 'dyw_e_u.txt' is a character vector, I don't understand why it return the error, please help, thanks
My text file is like this:
Lat Lon mm.dd.yyyy hh:mm:ss U(m^2/s) V(m^2/s) u(cm/s) v(cm/s) Depth(m)
22.4189 114.9908 2021/5/31 0:00:00 -1.123 1.949 -4.16 7.22 26.993
22.4189 114.9908 2021/5/31 1:00:00 -0.452 1.484 -1.675 5.497 26.993
22.4189 114.9908 2021/5/31 2:00:00 0.361 1.06 1.338 3.926 26.993
22.4189 114.9908 2021/5/31 3:00:00 0.994 0.838 3.684 3.104 26.993
22.4189 114.9908 2021/5/31 4:00:00 1.353 0.544 5.014 2.017 26.993
22.4189 114.9908 2021/5/31 5:00:00 1.597 -0.203 5.917 -0.754 26.993
22.4189 114.9908 2021/5/31 6:00:00 1.916 -1.429 7.097 -5.293 26.993
22.4189 114.9908 2021/5/31 7:00:00 2.289 -2.645 8.482 -9.798 26.993
22.4189 114.9908 2021/5/31 8:00:00 2.466 -3.196 9.137 -11.84 26.993

9 Comments

PLease attach the data file.
Thanks your reply, I attach the data file now
It's working here.
unzip('dyw_e_u.zip')
mat = readtable('dyw_e_u.txt')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
mat = 42295×11 table
Lat Lon mm_dd_yyyy hh_mm_ss U_m_2_s_ V_m_2_s_ u_cm_s_ v_cm_s_ Depth_m_ Var10 Var11 ______ ______ _____________ ________ ________ ________ _______ _______ ________ __________ __________ 22.419 114.99 {'2021/5/31'} 00:00:00 -1.123 1.949 -4.16 7.22 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 01:00:00 -0.452 1.484 -1.675 5.497 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 02:00:00 0.361 1.06 1.338 3.926 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 03:00:00 0.994 0.838 3.684 3.104 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 04:00:00 1.353 0.544 5.014 2.017 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 05:00:00 1.597 -0.203 5.917 -0.754 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 06:00:00 1.916 -1.429 7.097 -5.293 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 07:00:00 2.289 -2.645 8.482 -9.798 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 08:00:00 2.466 -3.196 9.137 -11.84 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 09:00:00 2.188 -2.792 8.104 -10.344 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 10:00:00 1.446 -1.748 5.355 -6.475 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 11:00:00 0.529 -0.712 1.958 -2.639 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 12:00:00 -0.198 -0.135 -0.734 -0.5 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 13:00:00 -0.576 0.045 -2.133 0.166 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 14:00:00 -0.745 0.222 -2.759 0.824 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 15:00:00 -0.964 0.658 -3.573 2.436 26.993 {0×0 char} {0×0 char}
What is the output when you run this snippet of code in your MATLAB?
which readtable -all
/MATLAB/toolbox/matlab/iofun/readtable.m /MATLAB/toolbox/shared/io/general/+matlab/+io/@ImportOptions/ImportOptions.m % matlab.io.text.DelimitedTextImportOptions method
I have no issue on my side (R2020b)
unzip dyw_e_u.zip
delete dyw_e_u.zip
a2=readtable('dyw_e_u.txt');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
head(a2)
Lat Lon mm_dd_yyyy hh_mm_ss U_m_2_s_ V_m_2_s_ u_cm_s_ v_cm_s_ Depth_m_ Var10 Var11 ______ ______ _____________ ________ ________ ________ _______ _______ ________ __________ __________ 22.419 114.99 {'2021/5/31'} 00:00:00 -1.123 1.949 -4.16 7.22 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 01:00:00 -0.452 1.484 -1.675 5.497 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 02:00:00 0.361 1.06 1.338 3.926 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 03:00:00 0.994 0.838 3.684 3.104 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 04:00:00 1.353 0.544 5.014 2.017 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 05:00:00 1.597 -0.203 5.917 -0.754 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 06:00:00 1.916 -1.429 7.097 -5.293 26.993 {0×0 char} {0×0 char} 22.419 114.99 {'2021/5/31'} 07:00:00 2.289 -2.645 8.482 -9.798 26.993 {0×0 char} {0×0 char}
As the others have shown, it's not a syntax issue; more than likely as @Dyuman Joshi was poking at you've aliased the readtable function accidentally.
Besides his suggestion to see what which returns, simply enter
clear readtable
at the command line and try again. That should fix it; if it doesn't, you may have somehow manged to corrupt the install, but that would be highly unlikely--not impossible, but not probable.
I fix it.
I try which and clear in the command line:
>> which readtable -all
D:\Program File\MATLAB\R2022b\toolbox\matlab\iofun\readtable.m
D:\Program File\MATLAB\R2022b\toolbox\shared\io\general\+matlab\+io\@ImportOptions\ImportOptions.m % matlab.io.ImportOptions method
>> clear readtable
>> which readtable -all
D:\Program File\MATLAB\R2022b\toolbox\matlab\iofun\readtable.m
D:\Program File\MATLAB\R2022b\toolbox\shared\io\general\+matlab\+io\@ImportOptions\ImportOptions.m % matlab.io.ImportOptions method
>> a2=readtable('dyw_e_u.txt');
Error using readtable
inputs must be a string array, character vector, or cell array of character vectors.
It still can't work.
I remember that the problem occur after I add a big folder and its subfolder to matlab's search path, so I remove it from search path and try again, now the readtable works! I try many times and finally locate the problem in a folder, if this folder in the search path, the readtable function can't work. But there are no functions or scripts name "readtable" in this folder. I put it here if anybody have interest to figure out why. Thanks everyone reply my question.
"if it is in the path the readtable function can't work. But there are no functions or scrips name readtable in this folder. I put it here if anybody have interest to figure out why. " <== that is not true. I took a file and moved it out of the current folder and into a different folder that is still in the search path. It found it and read it in.
Nonetheless we recommend that you not depend on the data folder being in the search path and for you to construct the full path (folder + base file name + extension) using the fullfile function. That way it will definitely find it.
help fullfile
FULLFILE Build full file name from parts. F = fullfile(FOLDERNAME1, FOLDERNAME2, ..., FILENAME) builds a full file specification F from the folders and file name specified. Input arguments FOLDERNAME1, FOLDERNAME2, etc. and FILENAME can be strings, character vectors, or cell arrays of character vectors. Non-scalar strings and cell arrays of character vectors must all be the same size. If any input is a string array, F is a string array. Otherwise, if any input is a cell array, F is a cell array. Otherwise, F is a character array. The output of FULLFILE is conceptually equivalent to character vector horzcat operation: F = [FOLDERNAME1 filesep FOLDERNAME2 filesep ... filesep FILENAME] except that care is taken to handle the cases when the folders begin or end with a file separator. FULLFILE collapses inner repeated file separators unless they appear at the beginning of the full file specification. FULLFILE also collapses relative folders indicated by the dot symbol, unless they appear at the end of the full file specification. Relative folders indicated by the double-dot symbol are not collapsed. To split a full file name into folder parts, use split(f, filesep). Examples % To build platform dependent paths to files: fullfile(matlabroot,'toolbox','matlab','general','Contents.m') % To build platform dependent paths to a folder: fullfile(matlabroot,'toolbox','matlab',filesep) % To build a collection of platform dependent paths to files: fullfile(toolboxdir('matlab'),'iofun',{'filesep.m';'fullfile.m'}) See also FILESEP, PATHSEP, FILEPARTS, GENPATH, PATH, SPLIT. Documentation for fullfile doc fullfile
I suggest you use
dbstop if error
and run your code, and then when it stops at the error line, start working backwards to trace the cause.
I am also not facing any issue in R2022b as well as R2023a.

Sign in to comment.

Answers (0)

Products

Release

R2022b

Asked:

on 11 Oct 2023

Edited:

on 12 Oct 2023

Community Treasure Hunt

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

Start Hunting!