MATLAB Answers


How can I use 'textscan' to read data from Sheet2 of an Excel file?

Asked by Tahir Ameen on 6 Jan 2019
Latest activity Edited by per isakson
on 7 Jan 2019
As said in question, want to know if textscan can be used to scan data from Sheet2 of an Excel file?
Thanks for any help.


Thanks Bob for giving me insight into the problem. I know about xlsread and readtable functions but I have got to use textscan in order to read/capture a time data (such as 12:34:56.123) from a csv file (or maybe an xlsx file later). I did use xlsread but then using xlswrite on a new xlsx file changed the format of the time into something like a fractional number (for instance .54321). So the point was to keep the format same; and using textscan/xlswrite combination applied on 'Sheet1' of my csv file did it perfectly - kept the format same. Having done this on Sheet1 with success, I am more interested now to apply it on Sheet2 but there doesn't seem to be a way to give an input argument like SheetNo. to textscan command.
Thanks anway.
@Tahir Ameen: CSV files do not have sheets. A CSV file is a text file and there is no way to define something like a sheet in it. Then neither textscan cannot have the power to select a sheet. Sheets are properties of binary XLS and XLSX files. There is no way to use a tool written to import text files of a certain format to import binary files on another format.
Summary: textscan imports text files only and text files do not have sheets. If you need to import sheets, use readtable or xlsread.
Now you reveal, that you are struggeling with dates. Of course this can be solved directly without the need of the time-consuming indirection over CSV files. Please post the current code and an example file. Then it is possible to suggest a solution.
Caveat: Personally I try to avoid Excel. What looks like datetime in Excel may be a character string or "datenum of Excel" presented as datetime. xlsread reads the first as a character string and the latter as a "strange number".

Sign in to comment.


1 Answer

Answer by per isakson
on 7 Jan 2019
 Accepted Answer

readtable( _______, 'DatetimeType', 'exceldatenum' );
See the documentation.


Sign in to comment.