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 个评论
Christian von Spreckelsen
I already tried renaming the extension to .txt (letter case) and removing the extra .txt, but without any success. I am not really sure what the nfts is?
Actually there is another pc with same problem when using my script. It is very perculiar. Two different pc's have a problem running this script and in both cases it is the .txt file it wont accept. But every other pc i shared the script with has no problem using it with the excact same .txt files and script :/
Walter Roberson
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
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 个评论
Christian von Spreckelsen
Thanks for the answer Walter!
All the pc's we ran the script on had Excel installed, including the pc's where the script didn't work.
But it's a great idea with the 'filetype'! I hadn't thought about that approach and that might just work.
I'll try it out later and confirm if it worked or not :)
Guillaume
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
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?
  1 个评论
Christian von Spreckelsen
Thanks for the answer Guillaume!
I actually tried looking behind the readtable code, but i didn't really know what to look for, so i came up with nothing :)
I'll check it out and report back, it might give some more insight in the actual problem.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by