Textscan with many requirements

5 ビュー (過去 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 Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by