Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Excel file reading

Subject: Excel file reading

From: Ashley Wright

Date: 13 May, 2010 02:57:04

Message: 1 of 5

I'm trying to read an excel file into matlab and call up a specific cell of data by its user defined row and column name. For example, with a simple 3x3 spreadsheet:

      Tree Grass Cat
Tree 1 2 3
Grass 4 5 6
Cat 7 8 9

I'd like the function to work by a user executing it by calling up example(tree, cat) which would output 3, or example(grass,tree) outputting 4. What's the best way to do this? Right now I'm using xlsread and assigning numbers to Tree, Grass, and Cat, but I want to be able to add more rows and columns later without having to modify the matlab code. Thanks for the help.

Subject: Excel file reading

From: TideMan

Date: 13 May, 2010 03:15:58

Message: 2 of 5

On May 13, 2:57 pm, "Ashley Wright" <navyas...@gmail.com> wrote:
> I'm trying to read an excel file into matlab and call up a specific cell of data by its user defined row and column name.  For example, with a simple 3x3 spreadsheet:
>
>       Tree    Grass     Cat
> Tree  1        2         3
> Grass  4        5         6
> Cat    7        8         9
>
> I'd like the function to work by a user executing it by calling up example(tree, cat) which would output 3, or example(grass,tree) outputting 4.  What's the best way to do this?  Right now I'm using xlsread and assigning numbers to Tree, Grass, and Cat, but I want to be able to add more rows and columns later without having to modify the matlab code.  Thanks for the help.

You say it is 3x3, but it's actually 4x4 as you've shown it.
Are the row and column headers in Excel, or not?
Have you read about the 2nd and 3rd output arguments in xlsread?

Subject: Excel file reading

From: Ashley Wright

Date: 13 May, 2010 03:30:22

Message: 3 of 5

TideMan <mulgor@gmail.com> wrote in message <8e3e53a3-24fa-4090-b9e8-428908ba2000@q36g2000prg.googlegroups.com>...
> On May 13, 2:57 pm, "Ashley Wright" <navyas...@gmail.com> wrote:
> > I'm trying to read an excel file into matlab and call up a specific cell of data by its user defined row and column name.  For example, with a simple 3x3 spreadsheet:
> >
> >       Tree    Grass     Cat
> > Tree  1        2         3
> > Grass  4        5         6
> > Cat    7        8         9
> >
> > I'd like the function to work by a user executing it by calling up example(tree, cat) which would output 3, or example(grass,tree) outputting 4.  What's the best way to do this?  Right now I'm using xlsread and assigning numbers to Tree, Grass, and Cat, but I want to be able to add more rows and columns later without having to modify the matlab code.  Thanks for the help.
>
> You say it is 3x3, but it's actually 4x4 as you've shown it.
> Are the row and column headers in Excel, or not?
> Have you read about the 2nd and 3rd output arguments in xlsread?

Tideman, yes you are correct, technically it is a 4x4. I meant 3x3 as far as the data goes. The row and column headers are in excel. Looking at the doc for xlsread, it says "xlsread ignores any leading row or column of text in the numeric result." Also, looking at example 4 in the doc shows that it puts the header data into a different variable which is not what I'm looking to do.

Subject: Excel file reading

From: TideMan

Date: 13 May, 2010 03:39:49

Message: 4 of 5

On May 13, 3:30 pm, "Ashley Wright" <navyas...@gmail.com> wrote:
> TideMan <mul...@gmail.com> wrote in message <8e3e53a3-24fa-4090-b9e8-428908ba2...@q36g2000prg.googlegroups.com>...
> > On May 13, 2:57 pm, "Ashley Wright" <navyas...@gmail.com> wrote:
> > > I'm trying to read an excel file into matlab and call up a specific cell of data by its user defined row and column name.  For example, with a simple 3x3 spreadsheet:
>
> > >       Tree    Grass     Cat
> > > Tree  1        2         3
> > > Grass  4        5         6
> > > Cat    7        8         9
>
> > > I'd like the function to work by a user executing it by calling up example(tree, cat) which would output 3, or example(grass,tree) outputting 4.  What's the best way to do this?  Right now I'm using xlsread and assigning numbers to Tree, Grass, and Cat, but I want to be able to add more rows and columns later without having to modify the matlab code.  Thanks for the help.
>
> > You say it is 3x3, but it's actually 4x4 as you've shown it.
> > Are the row and column headers in Excel, or not?
> > Have you read about the 2nd and 3rd output arguments in xlsread?
>
> Tideman, yes you are correct, technically it is a 4x4.  I meant 3x3 as far as the data goes.  The row and column headers are in excel.  Looking at the doc for xlsread, it says "xlsread ignores any leading row or column of text in the numeric result."  Also, looking at example 4 in the doc shows that it puts the header data into a different variable which is not what I'm looking to do.  

I beg to differ.
That is exactly what you want to do:
[a,b]=xlsread(xlsfile);
colhead=b(1,2:end);
rowhead=b(2:end,1);

Let's say inrow is the name of the row and incol is the name of the
column, then:
inrow='tree';incol='cat';
irow=strmatch(inrow,rowhead);
icol=strmatch(incol,colhead);

Now a(irow,icol) is the number you desire

Subject: Excel file reading

From: Ashley Wright

Date: 13 May, 2010 03:53:05

Message: 5 of 5

TideMan <mulgor@gmail.com> wrote in message <33815d06-2dcd-4b00-9664-3588436950de@u20g2000pru.googlegroups.com>...
> On May 13, 3:30 pm, "Ashley Wright" <navyas...@gmail.com> wrote:
> > TideMan <mul...@gmail.com> wrote in message <8e3e53a3-24fa-4090-b9e8-428908ba2...@q36g2000prg.googlegroups.com>...
> > > On May 13, 2:57 pm, "Ashley Wright" <navyas...@gmail.com> wrote:
> > > > I'm trying to read an excel file into matlab and call up a specific cell of data by its user defined row and column name.  For example, with a simple 3x3 spreadsheet:
> >
> > > >       Tree    Grass     Cat
> > > > Tree  1        2         3
> > > > Grass  4        5         6
> > > > Cat    7        8         9
> >
> > > > I'd like the function to work by a user executing it by calling up example(tree, cat) which would output 3, or example(grass,tree) outputting 4.  What's the best way to do this?  Right now I'm using xlsread and assigning numbers to Tree, Grass, and Cat, but I want to be able to add more rows and columns later without having to modify the matlab code.  Thanks for the help.
> >
> > > You say it is 3x3, but it's actually 4x4 as you've shown it.
> > > Are the row and column headers in Excel, or not?
> > > Have you read about the 2nd and 3rd output arguments in xlsread?
> >
> > Tideman, yes you are correct, technically it is a 4x4.  I meant 3x3 as far as the data goes.  The row and column headers are in excel.  Looking at the doc for xlsread, it says "xlsread ignores any leading row or column of text in the numeric result."  Also, looking at example 4 in the doc shows that it puts the header data into a different variable which is not what I'm looking to do.  
>
> I beg to differ.
> That is exactly what you want to do:
> [a,b]=xlsread(xlsfile);
> colhead=b(1,2:end);
> rowhead=b(2:end,1);
>
> Let's say inrow is the name of the row and incol is the name of the
> column, then:
> inrow='tree';incol='cat';
> irow=strmatch(inrow,rowhead);
> icol=strmatch(incol,colhead);
>
> Now a(irow,icol) is the number you desire

Outstanding, worked like a charm. Thanks for the help. I guess I misunderstood how the headers were assigned.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us