Clear Filters
Clear Filters

Importing a table with unknown number of rows

8 views (last 30 days)
I have several xlsx files that contain the same columns, but different number of rows. An example xlsx file is attached. I would like to import them as tables, so I used Import Wizard to generate the attached function. The problem with the function is that it specifies the row length in opts.DataRange. However I would like to use this function on other xlxs files with the same columns, but different number of rows. How do I edit this function so it can accept any number of rows? Here's what I've tried,
dataLines = [2, Inf]; % Want to go from the 2th row to the last unknown row. Doesn't work.
opts.DataRange = "A" + dataLines(1, 1) + ":C" + dataLines(1, 2); % Columns A to C are imported from Excel file
but it crashes. I also tried
dataLines = [2, 500]; % I happen to know I always have less than 500 rows
but then my table actually has 500 rows, with the last several rows undesirably empty. I could manually delete them but I suspect there's a better way.

Accepted Answer

Stephan
Stephan on 16 Apr 2021
I dont think it is needed to specify this, using readtable function. I never used this option to import a .csv-file as table in Matlab.
  5 Comments
Stephan
Stephan on 17 Apr 2021
Its a one liner:
>> myTable = readtable('example_cooking_workbook.xlsx','PreserveVariableNames',true)
myTable =
39×3 table
Technique Serve Date Dish Number
_____________ ___________ ___________
{'salt' } 23-Jun-1980 57
{'saute' } 18-Jun-1992 93
{'scald' } 07-Jun-1994 79
{'scallop' } 18-Mar-1994 77
{'scoop' } 02-Aug-1996 57
{'score' } 17-Sep-1994 93
{'scramble' } 18-Mar-1994 81
{'sear' } 11-Apr-1997 68
{'season' } 17-Oct-1996 76
{'separate' } 10-Apr-1998 70
{'serve' } 12-Aug-1999 54
{'shell' } 23-Jul-1999 62
{'shirr' } 07-Feb-2000 56
{'shred' } 07-Feb-2001 59
{'shuck' } 03-Aug-2001 62
{'sieve' } 28-Nov-2000 71
{'sift' } 18-Dec-2000 52
{'simmer' } 03-Dec-1999 95
{'skewer' } 21-Sep-2001 97
{'skim' } 19-Dec-2003 75
{'skin' } 12-Aug-2004 74
{'slice' } 16-Nov-2004 67
{'sliver' } 29-Jan-2004 95
{'slow cook'} 14-Mar-2005 68
{'smoke' } 23-Mar-2005 56
{'snip' } 06-May-2004 89
{'soak' } 23-Mar-2005 69
{'soft boil'} 17-Nov-2004 62
{'souse' } 28-Jul-2008 70
{'sprinkle' } 28-Mar-2008 55
{'steam' } 19-Jun-2008 57
{'steep' } 29-Nov-2007 97
{'stew' } 30-Mar-2012 98
{'stir' } 04-Oct-2010 79
{'stir fry' } 30-Aug-2010 53
{'strain' } 15-Dec-2011 62
{'stuff' } 31-Aug-2011 68
{'sweat' } 27-Feb-2013 91
{'sweeten' } 24-Aug-2012 51
KAE
KAE on 18 Apr 2021
Thanks! I wonder why the Import Wizard function generation makes it so complicated? Well, serves me right for relying on it.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!