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:
filling a matrix

Subject: filling a matrix

From: Jonathan

Date: 21 Nov, 2012 01:38:16

Message: 1 of 3

G'day,
I'm trying to read in hourly data from multiple years and create a matrix that incrementally adds each year of data to the same matrix. The code I have is:

j1 = [1975,1977,1978,1979,1981,1982,1983,1985,1986,1987,...
    1989,1990,1991,1993,1994,1995,1997,1998,1999,2001,2002,2003,...
    2005,2006,2007,2009,2010,2011]; %normal years
j2 = [1928,1932,1936,1940,1944,1948,1952,1956,1960,1964,1968,1972,...
    1976,1980,1984,1988,1992,1996,2000,2004,2008,2012]; %leap years

tst=1987:1989;
N=length(tst);
A = NaN*ones(N,8784); %fill with NaNs

for m=1987:1989; % add hourly data
    for j=1:N;
        fname=sprintf('yaq%d.txt',m);
        if any(m == j1);
            data=load(fname);
            Hs=data(:,5);
            A(j,1:length(Hs)) = Hs;
        elseif any(m == j2);
            data=load(fname);
            Hs=data(:,5);
            A(j,1:length(Hs)) = Hs;
        end
    end
end

However, my code seems to be only repeating the last year of data. I know its a simple fix but I'm having a mind block. What am I doing wrong?

Thanks.

Subject: filling a matrix

From: Roger Stafford

Date: 21 Nov, 2012 03:11:16

Message: 2 of 3

"Jonathan " <jkakiwi@yahoo.co.uk> wrote in message <k8hba8$9co$1@newscl01ah.mathworks.com>...
> G'day,
> I'm trying to read in hourly data from multiple years and create a matrix that incrementally adds each year of data to the same matrix. The code I have is:
>
> j1 = [1975,1977,1978,1979,1981,1982,1983,1985,1986,1987,...
> 1989,1990,1991,1993,1994,1995,1997,1998,1999,2001,2002,2003,...
> 2005,2006,2007,2009,2010,2011]; %normal years
> j2 = [1928,1932,1936,1940,1944,1948,1952,1956,1960,1964,1968,1972,...
> 1976,1980,1984,1988,1992,1996,2000,2004,2008,2012]; %leap years
>
> tst=1987:1989;
> N=length(tst);
> A = NaN*ones(N,8784); %fill with NaNs
>
> for m=1987:1989; % add hourly data
> for j=1:N;
> fname=sprintf('yaq%d.txt',m);
> if any(m == j1);
> data=load(fname);
> Hs=data(:,5);
> A(j,1:length(Hs)) = Hs;
> elseif any(m == j2);
> data=load(fname);
> Hs=data(:,5);
> A(j,1:length(Hs)) = Hs;
> end
> end
> end
>
> However, my code seems to be only repeating the last year of data. I know its a simple fix but I'm having a mind block. What am I doing wrong?
>
> Thanks.
- - - - - - - - -
  I see (at least) two problems here. First you have one too many for-loops. You ought to do something like this:

 A = NaN*ones(N,8784); %fill with NaNs
 tst=1987:1989;
 for j = 1:length(tst)
   m = tst(j); % <-- Use this method of varying m, not a second nested loop
   fname=sprintf('yaq%d.txt',m);
   (Now store 'fname' data in A)
 end

  The second problem is that your choice between "normal years" and "leap years" is not affecting any result in A here. Both parts of the if-elseif-end choice do exactly the same thing. You presumably had something other than this in mind here.

Roger Stafford

Subject: filling a matrix

From: Jonathan

Date: 21 Nov, 2012 17:09:08

Message: 3 of 3

"Roger Stafford" wrote in message <k8hgok$qm2$1@newscl01ah.mathworks.com>...
> "Jonathan " <jkakiwi@yahoo.co.uk> wrote in message <k8hba8$9co$1@newscl01ah.mathworks.com>...
> > G'day,
> > I'm trying to read in hourly data from multiple years and create a matrix that incrementally adds each year of data to the same matrix. The code I have is:
> >
> > j1 = [1975,1977,1978,1979,1981,1982,1983,1985,1986,1987,...
> > 1989,1990,1991,1993,1994,1995,1997,1998,1999,2001,2002,2003,...
> > 2005,2006,2007,2009,2010,2011]; %normal years
> > j2 = [1928,1932,1936,1940,1944,1948,1952,1956,1960,1964,1968,1972,...
> > 1976,1980,1984,1988,1992,1996,2000,2004,2008,2012]; %leap years
> >
> > tst=1987:1989;
> > N=length(tst);
> > A = NaN*ones(N,8784); %fill with NaNs
> >
> > for m=1987:1989; % add hourly data
> > for j=1:N;
> > fname=sprintf('yaq%d.txt',m);
> > if any(m == j1);
> > data=load(fname);
> > Hs=data(:,5);
> > A(j,1:length(Hs)) = Hs;
> > elseif any(m == j2);
> > data=load(fname);
> > Hs=data(:,5);
> > A(j,1:length(Hs)) = Hs;
> > end
> > end
> > end
> >
> > However, my code seems to be only repeating the last year of data. I know its a simple fix but I'm having a mind block. What am I doing wrong?
> >
> > Thanks.
> - - - - - - - - -
> I see (at least) two problems here. First you have one too many for-loops. You ought to do something like this:
>
> A = NaN*ones(N,8784); %fill with NaNs
> tst=1987:1989;
> for j = 1:length(tst)
> m = tst(j); % <-- Use this method of varying m, not a second nested loop
> fname=sprintf('yaq%d.txt',m);
> (Now store 'fname' data in A)
> end
>
> The second problem is that your choice between "normal years" and "leap years" is not affecting any result in A here. Both parts of the if-elseif-end choice do exactly the same thing. You presumably had something other than this in mind here.
>
> Roger Stafford

Thanks Roger, this worked.

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