Why is xlsread not working with text file? Documentation says it should work with other any file that Excel can open.
2 次查看(过去 30 天)
显示 更早的评论
Documentation for the xlsread command says:
" On Windows® systems with Microsoft Excel software, xlsread reads any file format recognized by your version of Excel. "
So that sounds to me like xlsread should be able to open some .txt files that I exported from AFM software. The text file consists of a column header and a column of numbers written in scientific notation. I can open said text files directly in Excel if I use the Open with -> command in the right-click context menu in Windows. Excel interprets the contents cleanly, placing the header text at the top of the column and each subsequent number into a cell.
However, when I try to use xlsread to read the text file, I get the error: File my file.txt not in Microsoft Excel Format. (I substituted "my file" for the actual name.)
0 个评论
采纳的回答
per isakson
2014-8-1
编辑:per isakson
2014-8-1
My system: R20113a, 64bit, Win7, Excel2010
 
My first trial resulted in
Error using xlsread (line 247)
File c:\tmp\1.5% PVAc 500nm SS 1.5Hz SR 5nm lift 28C.116.txt
not in Microsoft Excel Format.
the line causing the error is
openExcelWorkbook(Excel, file, readOnly);
Next, I replaced .txt by .csv and tried again
Name Size Bytes Class Attributes
alldata 16385x1 2818220 cell
filespec 1x55 110 char
ndata 0x0 0 double
text 16385x1 2818220 cell
where my script is
filespec = 'c:\tmp\1.5% PVAc 500nm SS 1.5Hz SR 5nm lift 28C.116.csv';
[ndata, text, alldata] = xlsread(filespec);
whos
Why ndata is empty I cannot guess. Ok, it's the way Excel works.
 
recognized by your version of Excel   There seems to be two levels of recognizing. I tried to open the file with the two different extensions from inside Excel. With .txt the interactive Wizard was invoked and with .csv it worked automagically. However, the magic didn't recognize the "numerical data" as numeric.
 
 
 
Yes, there certainly are better ways to read this file, e.g. textscan
2 个评论
per isakson
2014-8-2
编辑:per isakson
2014-8-2
textscan is my first choice when reading text files containing some headerlines together with numerical (and text) data in columns. xlsread is not on the list. It give you more control and it is relatively fast, however it requires a tiny bit more reading of the manual. When textscan fails it is easier to understand why. importdata is a wrapper to textscan. Little demo:
>> tic, val = cssm(); toc, whos
Elapsed time is 0.063142 seconds.
Name Size Bytes Class Attributes
val 16384x1 131072 double
where
function val = cssm
filespec ...
= 'c:\tmp\1.5% PVAc 500nm SS 1.5Hz SR 5nm lift 28C.116.txt';
fid = fopen( filespec );
cac = textscan( fid, '%f', 'Headerlines', 1 );
fclose(fid );
val = cac{1};
end
更多回答(1 个)
Image Analyst
2014-7-31
Try something else, like importdata() or textscan() or readtable().
16 个评论
Star Strider
2014-8-1
To rename them in a script, movefile is likely your best friend. See Examples —> Renaming a File in the Current Folder.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!