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 CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by