Textscan with many requirements

2 次查看(过去 30 天)
Madlab
Madlab 2018-9-24
评论: Stephen23 2018-9-24
I’ve tried all sorts of methods, from textscan to textread but I am struggling to read the excel file. Only allowed to use textscan/textread, not xlsread. I've utilized whatever forum question I came across, but all did not help me much.
Right now, I am getting "Too many output arguments." Even after I limit to [Ar] = textscan(filepath,...etcetc); , what I get is not all the data I want. I would like to read every data available.
filepath = 'file.xls';
[Ar,B,C,D,E,F,G,H,I,J,K,L,M] = textscan(filepath,'%f %c %c %c %c %{yyyy}D %c %c %f %f %f %c %c','headerlines',2,'delimiter',',','emptyvalue',NaN);

回答(2 个)

Walter Roberson
Walter Roberson 2018-9-24
编辑:Walter Roberson 2018-9-24
You have attached a file with an xls file extension. Xls files are always binary files that would be rather difficult to process with textscan as that routine deals with text files.
The content of the file you attached is not either an xls file nor an xlsx file. It is an XML file created by Microsoft Word. It might even be the key XML file that would be present inside an xlsx file (xlsx are zipped files of directories of XML documents).
It is not especially easy to use textscan to parse an XML document, but you could probably extract key information from it if you were persistent enough.
If you have an imposed requirement to use textscan to read the contents then you could use a %s format with whitespace and delimiter set empty to just read everything as one string. Then pass the string to an XML parser, or use regexp to parse it. This would stay within the letter of the requirement while completely violating the spirit of the requirement, perhaps, but trying to use textscan to parse an XML file is not worth the effort unless the point of the exercise is to become a textscan expert past all reasonable textscan use.
  6 个评论
Walter Roberson
Walter Roberson 2018-9-24
Missing rows at the end usually mean that the file format does not match the format you are scanning with.
Madlab
Madlab 2018-9-24
I got a 1x13 cell. The number of columns are right, but the rows are way off.

请先登录,再进行评论。


Madlab
Madlab 2018-9-24
编辑:Madlab 2018-9-24
Note: I am now trying to read with textread.
[v,co,p,a,t,lst,long,elev,rock,set] = textread(inFile,'%*f %q %q %q %d %q %*q %*q %d %d %d %q %q','headerlines',2,'delimiter',',','emptyvalue',NaN);
The issue is, there are some location names with a comma which messes up the delimiter...and I have trouble like "Trouble reading integer from file (row 1, field 5) ==> Eruption Dated,8300 BCE,Mediterranean and Wes"
  1 个评论
Stephen23
Stephen23 2018-9-24
Note: at the very top of the textread help page it states:
" textread is not recommended. Use textscan instead."

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by