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:
break a loop when reading different excel files

Subject: break a loop when reading different excel files

From: Saad

Date: 26 Nov, 2012 14:31:11

Message: 1 of 3

Dear all,

I am trying to read different excel files from 2010 to 2012. Basically, for each month, there is one excel file. So when i run the loop I go through each month and read the excel file of that particular. The loop works with no problem for 2010 and 2011 but it stops at the end of 2012 because the data for November and December 2012 are not available yet. Here is my code:

months={'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'};

    for i=2010:2012

        for j=1:length(months)
            
           
 a1=xlsread(['P:\X\Data\', num2str(i),'\', char(months(j)), num2str(i), '.xls'],'Sheet1','X5:X5000');

                        
            
        end
        
     end

As you can see the loop reads the excel file at each month but stops at end 2012. I would like to end (break) the loop when Matlab doesnt find the relevant excel file. How can I do that? Thank you very much for your help

Kind REgards

S

Subject: break a loop when reading different excel files

From: dpb

Date: 26 Nov, 2012 15:28:57

Message: 2 of 3

On 11/26/2012 8:31 AM, Saad wrote:
> Dear all,
>
> I am trying to read different excel files from 2010 to 2012. Basically,
> for each month, there is one excel file. So when i run the loop I go
> through each month and read the excel file of that particular. The loop
> works with no problem for 2010 and 2011 but it stops at the end of 2012
> because the data for November and December 2012 are not available yet.
> Here is my code:
>
> months={'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
> 'Oct', 'Nov', 'Dec'};
>
> for i=2010:2012
> for j=1:length(months)
> a1=xlsread(['P:\X\Data\', num2str(i),'\', char(months(j)), num2str(i),
> '.xls'],'Sheet1','X5:X5000');
>
> end
> end
>
> As you can see the loop reads the excel file at each month but stops at
> end 2012. I would like to end (break) the loop when Matlab doesnt find
> the relevant excel file. How can I do that? Thank you very much for your
> help

Several ways...

a) enclose the call to xlsread() in a try...catch error block and handle
the error

b) do a dir() on the file name before try to open to test for existence
and break if not found,

c) use dir() on the file name w/ appropriate wild card and the process
the resulting files so only have existing files to deal with instead.
Has secondary benefit of not needing the sprintf() stuff to create the
filename manually..

--

Subject: break a loop when reading different excel files

From: Saad

Date: 26 Nov, 2012 18:29:19

Message: 3 of 3

dpb <none@non.net> wrote in message <k901th$vsa$1@speranza.aioe.org>...
> On 11/26/2012 8:31 AM, Saad wrote:
> > Dear all,
> >
> > I am trying to read different excel files from 2010 to 2012. Basically,
> > for each month, there is one excel file. So when i run the loop I go
> > through each month and read the excel file of that particular. The loop
> > works with no problem for 2010 and 2011 but it stops at the end of 2012
> > because the data for November and December 2012 are not available yet.
> > Here is my code:
> >
> > months={'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
> > 'Oct', 'Nov', 'Dec'};
> >
> > for i=2010:2012
> > for j=1:length(months)
> > a1=xlsread(['P:\X\Data\', num2str(i),'\', char(months(j)), num2str(i),
> > '.xls'],'Sheet1','X5:X5000');
> >
> > end
> > end
> >
> > As you can see the loop reads the excel file at each month but stops at
> > end 2012. I would like to end (break) the loop when Matlab doesnt find
> > the relevant excel file. How can I do that? Thank you very much for your
> > help
>
> Several ways...
>
> a) enclose the call to xlsread() in a try...catch error block and handle
> the error
>
> b) do a dir() on the file name before try to open to test for existence
> and break if not found,
>
> c) use dir() on the file name w/ appropriate wild card and the process
> the resulting files so only have existing files to deal with instead.
> Has secondary benefit of not needing the sprintf() stuff to create the
> filename manually..
>
> --

Thanks so much for the quick answer!

Tags for 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