Code covered by the BSD License  

Highlights from
xls2struct

5.0

5.0 | 1 rating Rate this file 8 Downloads (last 30 days) File Size: 8.2 KB File ID: #19707

xls2struct

by Ashish Uthama

 

24 Apr 2008 (Updated 20 May 2010)

Load Excel file contents as a structure

| Watch this File

File Information
Description

 xls2struct Load Excel file contents as a structure
  xlsstruct = xls2struct(file) loads the Excel file 'file'. The first row is
  used to generate the field names for the 1x1 structure xlsstruct. Each
  column (excluding the first row) is then converted to a numeric/cell
  array and assigned to the corresponding field name. All columns are
  expected to have equal number of entries.
 
  xlsstructArray = xls2struct(file,'structArray') will return a 1xNumCol
  structure array xlsstructArray. The kth element of the structure will
  have field names corresponding to the first row and values corresponding
  to the kth row.
 
  GENVARNAME is used to generate a valid MATLAB structure field name from
  the first row data.
 
  Blank entries are returned as 'NaN'.
 
  Platform support is dependent on XLSREAD.
 
  %Example: Given this Excel file content (9 columns, 5 rows):
 
  %one two three four ' ' six se ven
  %1 2 3 6 7
  %11 22 three
  % seven
  % 222 33
 
  xlsStruct=xls2struct('example.xls')
  % Where:
  % xlsStruct.one' = 1 11 NaN NaN
  % xlsStruct.two' = 2 22 NaN 222
  % xlsStruct.three' = [3] 'three' [NaN] [33]
  % xlsStruct.four' = NaN NaN NaN NaN
  % xlsStruct.x' = NaN NaN NaN NaN
  % xlsStruct.six' = 6 NaN NaN NaN
  % xlsStruct.seVen' = [7] [NaN] 'seven' [NaN]
 
  xlsStructArray=xls2struct('example.xls','structArray')
  % Where:
  % xlsStructArray =
  %
  % 1x4 struct array with fields:
  % one
  % two
  % three
  % four
  % x
  % six
  % seVen

MATLAB release MATLAB 7.10 (2010a)
Other requirements Not tested on non-windows platform.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
01 Jun 2011 Lube F

Thanks, looks great. Given it has the same name as XLS2STRUCT by Andreas 29 Oct 2003 (Updated 28 Jan 2005) it's a pity it doesn't have the same functionality i.e. the option to specify the worksheet! It's easy to edit the function myself, but it would be great if others didn't have to. Cheers.

12 Jul 2011 Srikar

I guess there is a small bug in the code. Try creating first column with numbers, second with strings and third with numbers..

This part needs a change to fix the bug:
        %this column only contains numbers, use arrays
        %varData=num(:,numColInd);
        varData=num(:,varInd);
        numColInd=numColInd+1;

Please login to add a comment or rating.
Updates
29 Dec 2008

*Use dynamic structure field names instead of eval
*More flexibility for input data format in excel sheet

27 Apr 2010

* suppressed inconsequential mlints
* fixed bug while handling mixed type of data ( ex: 1 2 'a' 'b')

28 Apr 2010

* significant rewrite - add support to return structure arrays

20 May 2010

* Fixed bug when first column is all text. (num matrix returned by XLSREAD has one less column)

Tag Activity for this File
Tag Applied By Date/Time
data import Ashish Uthama 22 Oct 2008 09:59:10
data export Ashish Uthama 22 Oct 2008 09:59:10
xls excel read variables Ashish Uthama 22 Oct 2008 09:59:10
xls Ashish Uthama 28 Apr 2010 09:45:41
structure Ashish Uthama 28 Apr 2010 09:45:41
io Ashish Uthama 28 Apr 2010 09:45:41
excel Ashish Uthama 28 Apr 2010 09:45:41
data export Nizar 20 May 2010 11:09:41

Contact us at files@mathworks.com