readfromexcel

Returns values in multiple ranges of existing Excel file.
11K Downloads
Updated 5 Aug 2004

View License

VARARGOUT = READFROMEXCEL(FILELOC,VARARGIN)

Uses ActiveX commands to read range(s) from an existing Excel spreadsheet.

FILELOC: Enter a string representing the (absolute or relative) location of an Excel file. (Extension may be omitted, and will be assumed to be .xls.)
Examples: 'c:\brett\my archives\test1.xls'
'test1.xls'
'myarchive'

SHEETNAME: (Optional): Any occurrence in the variable argument list of the strings 'sheetname' or 'sheet' prompts the function to change active sheets to the value in the following variable. That specifier must be a string argument matching exactly the name of an existing sheet in the opened file. If this argument is omitted, the function defaults to reading from the currently active sheet.

RANGE SPECIFIER(S): Enter the range(s) to read. The values stored in these ranges will be returned in consecutive output arguments.
Example: 'B1:B5'
'B1:B1' (or simply 'B1')
'B1:P4'
'B:B' or 'B'
(Entire second column)
'2:2' or '2'
(Entire second row)
'ALL' (Entire sheet)

(Additional ranges: Comma separated ranges in the same form as above; contents of archive will be returned in output arguments 2...n)

OUTPUT: If specified range is 1 cell, variable returned is of the same class as cell contents. If the range spans more than 1 cell, the variables will be cell arrays.

EXAMPLES:
a = readfromexcel('c:\brett\my archives\test1.xls','C1:C5');
reads from the currently active sheet

a = readfromexcel('c:\brett\my archives\test1.xls', 'ALL');
reads the entire used portion of the active sheet

a = readfromexcel('c:\brett\my archives\test1.xls', 'C');
reads column C of the used portion of the active sheet

[a,b] = readfromexcel('c:\brett\my archives\test1.xls','sheet','sheet2','C1:C5','C1:P3');
reads from sheet2

[a,b,c] = readfromexcel('myarchive','C3:D5','sheet','mysheet','E4','sheet','sheet2','B3');
reads a from currently active sheet, switches to sheet 'mysheet' to read b, then to sheet 'sheet2' to read c.

Written by Brett Shoelson, Ph.D.
shoelson@helix.nih.gov
Update History:
1/04. Version 1.
2/2/04. Now allows multiple specifications of sheet name at the suggestion of R. Venkat), and support of relative paths (thanks to Urs Schwarz). Also, inclusion of the extension '.xls' is now superfluous.
7/29/04. Implements try/catch structure for reading of ranges to avoid errors that leave open activex connections. (Response to Chris Paterson's CSSM query). Also, now accomodates reading of entire sheet, or of entire row/column. (Response to email queries by Kinan Rai and CSSM query by Xiong.)

SEE ALSO: write2excel

Cite As

Brett Shoelson (2024). readfromexcel (https://www.mathworks.com/matlabcentral/fileexchange/4415-readfromexcel), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: Cell 'Find And Replace'

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0.0

Allows more robust range selection, including 'ALL' (for entire used portion of sheet), or 'B:B' or '7:7' (for entire second column or seventh row). Also, more robust try/catch structure for fewer orphaned processes.