Error using readtable. File extension txt not recognized
37 次查看(过去 30 天)
显示 更早的评论
Hi
I made a script for loading a .txt file and doing some calculations on the loaded data. It works fine for me and several other i shared the script with, but with a couple of people it doesn't work. When they run the script and choose the file to load, they get following message
He is trying to load the excact same file that works for everyone else. The file is attached to this post. Anyone who had a similiar problem or knows how to fix this?
EDIT: He has no problem loading the files when using the "Import Data" button from the toolbar, only when it is through readtable in a script he got the problem. Besides that we just found out he has got the 2015b version, where mine is 2016b. But it seems unlikely that readtable didn't know how to load a .txt file before 2016
5 个评论
Walter Roberson
2016-12-5
Sorry, nfts should have been NTFS, the main Windows File System. But it sounds like that is not the issue.
回答(2 个)
Walter Roberson
2016-12-5
Try adding 'filetype', 'text' to the readtable() call. I am looking at the R2015b documentation and it certainly should support .txt extension, but give it the hint.
Question: the systems that do not work, do they have Excel installed or not? If they do not then notice 'basic'
Indicator for reading in basic mode, specified as the comma-separated pair consisting of 'Basic' and either true, false, 1, or 0.
basic mode is the default for systems without Excel for Windows. In basic mode, readtable:
- Reads XLS, XLSX, XLSM, XLTX, and XLTM files only.
2 个评论
Guillaume
2016-12-9
'Filetype', 'text' should indeed fix the problem.
For text files, the presence or lack of excel does not matter and 'Basic' has no effect as readtable uses textscan to parse the file in any case.
Guillaume
2016-12-9
You can look at the code that readtable uses to detect the filetype (in matlabroot\toolbox\matlab\datatypes\@table\readFromFile.m), it's not complicated.
You'll get an error if
[~, ~, fx] = fileparts(filename);
ismember(lower(fx), {'.txt' '.dat' '.csv'})
returns false. So what does lower(fx) return on those system where it does not work?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!