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:
loop question

Subject: loop question

From: Jonathan

Date: 2 Mar, 2009 21:05:19

Message: 1 of 3

Hi
I have been struggling with trying to write a simple script that loops, x times.

What I have so far is...

fname=sprintf('Orford 9431647.hh.xls',i);

j1 = [1978,1979,1981,1982,1983,1985,1986,1987,1989,1990,1991,1993,1994,...
    1995,1997,1998,1999,2001,2002,2003,2005,2006,2007]; %years
j2 = [1980,1984,1988,1992,1996,2000,2004,2008]; %leap years

for m=1978:1:2008;
    fname2=sprintf('%d',m);
    if m == j1;
        run temp1.m %years
    elseif m == j2;
        run temp2.m %leap years
    end
end

what I am trying to do is to automate two other scripts (temp1 and temp2) that compile simple statistics on hourly tide data. The scripts simply read in the hourly data (stored as yearly files) and then outputs a summary file. I am stuck and would appreciate some guidance on how to do this.

Thanks
Jon

Subject: loop question

From: Eric W

Date: 2 Mar, 2009 21:17:01

Message: 2 of 3

"Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <gohhmf$lob$1@fred.mathworks.com>...
> Hi
> I have been struggling with trying to write a simple script that loops, x times.
>
> What I have so far is...
>
> fname=sprintf('Orford 9431647.hh.xls',i);
>
> j1 = [1978,1979,1981,1982,1983,1985,1986,1987,1989,1990,1991,1993,1994,...
> 1995,1997,1998,1999,2001,2002,2003,2005,2006,2007]; %years
> j2 = [1980,1984,1988,1992,1996,2000,2004,2008]; %leap years
>
> for m=1978:1:2008;
> fname2=sprintf('%d',m);
> if m == j1;
> run temp1.m %years
> elseif m == j2;
> run temp2.m %leap years
> end
> end
>
> what I am trying to do is to automate two other scripts (temp1 and temp2) that compile simple statistics on hourly tide data. The scripts simply read in the hourly data (stored as yearly files) and then outputs a summary file. I am stuck and would appreciate some guidance on how to do this.
>
> Thanks
> Jon

You need to use any() with the conditions on the if() statements.

j1 = [1978,1979,1981,1982,1983,1985,1986,1987,1989,1990,1991,1993,1994,...
    1995,1997,1998,1999,2001,2002,2003,2005,2006,2007]; %years
j2 = [1980,1984,1988,1992,1996,2000,2004,2008]; %leap years

for m=1978:1:2008;
    fname2=sprintf('%d',m);
    if any(m == j1)
        run temp1.m %years
    elseif any(m == j2)
        run temp2.m %leap years
else
disp('It''s been a rough year');
end
end

Subject: loop question

From: Jonathan

Date: 2 Mar, 2009 22:33:01

Message: 3 of 3

Thanks, problem solved.

"Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <gohhmf$lob$1@fred.mathworks.com>...
> Hi
> I have been struggling with trying to write a simple script that loops, x times.
>
> What I have so far is...
>
> fname=sprintf('Orford 9431647.hh.xls',i);
>
> j1 = [1978,1979,1981,1982,1983,1985,1986,1987,1989,1990,1991,1993,1994,...
> 1995,1997,1998,1999,2001,2002,2003,2005,2006,2007]; %years
> j2 = [1980,1984,1988,1992,1996,2000,2004,2008]; %leap years
>
> for m=1978:1:2008;
> fname2=sprintf('%d',m);
> if m == j1;
> run temp1.m %years
> elseif m == j2;
> run temp2.m %leap years
> end
> end
>
> what I am trying to do is to automate two other scripts (temp1 and temp2) that compile simple statistics on hourly tide data. The scripts simply read in the hourly data (stored as yearly files) and then outputs a summary file. I am stuck and would appreciate some guidance on how to do this.
>
> Thanks
> Jon

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