Error using readtable. File extension txt not recognized
조회 수: 30 (최근 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158664/image.jpeg)
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!