Textscan with many requirements

5 views (last 30 days)
Madlab
Madlab on 24 Sep 2018
Commented: Stephen23 on 24 Sep 2018
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);

Answers (2)

Walter Roberson
Walter Roberson on 24 Sep 2018
Edited: Walter Roberson on 24 Sep 2018
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 Comments
Walter Roberson
Walter Roberson on 24 Sep 2018
Missing rows at the end usually mean that the file format does not match the format you are scanning with.
Madlab
Madlab on 24 Sep 2018
I got a 1x13 cell. The number of columns are right, but the rows are way off.

Sign in to comment.


Madlab
Madlab on 24 Sep 2018
Edited: Madlab on 24 Sep 2018
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 Comment
Stephen23
Stephen23 on 24 Sep 2018
Note: at the very top of the textread help page it states:
" textread is not recommended. Use textscan instead."

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!