![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175332/image.png)
Why is xlsread not working with text file? Documentation says it should work with other any file that Excel can open.
1 回表示 (過去 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.
 
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175332/image.png)
 
 
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 で Data Import and Export についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!