Prev: how to import data from database to matlab - PLEASE HELP!
Next: mexMakeMemoryPersistent with mxRealloc
From: Maciej O on 15 Jul 2010 19:20 Dear all, I'm very new to Matlab and struggling with completing my code for shortwave radiation. I'm not sure how to record/save/keep the data (for further calculations) from within the loop before it gets overwritten during the subsequent loop. Basically, the result should consists of 8760 (365*24) entries; one yearly oscillation (365 days) composed of daily (24hr) oscillations here is a simplified running version of the code: lat = -77.024722; rad = 1*(pi/180); %degrees to radians lat_r = lat*rad; fc = 360*rad; % 360 degrees in radians y = 365; %year in days for J = 1:365; decl = 23.44 * rad * sin(fc*((J+284)/y)); for hr = 1:24; HA = (12-(hr))*pi/12; HA_rad = HA * (pi/180); z = acos(sin(decl)*sin(lat_r)+ cos(lat_r)*cos(decl)*cos(HA_rad)); results (hr) = 1376*(1-.48*1)*cos(z)*(.3+.73*cos(z)); end end I've tried save -append function but it didn't work for me. Maybe it's just my syntax. I'm sure this is simple but for some reason I just can't make it happen. Thanks.
From: dpb on 15 Jul 2010 21:09 Maciej O wrote: > Dear all, > > I'm very new to Matlab and struggling with completing my code for > shortwave radiation. > I'm not sure how to record/save/keep the data (for further calculations) > from within the loop before it gets overwritten during the subsequent > loop. That's what arrays are for... > ... Basically, the result should consists of 8760 (365*24) entries; > one yearly oscillation (365 days) composed of daily (24hr) oscillations > > here is a simplified running version of the code: > lat = -77.024722; > rad = 1*(pi/180); %degrees to radians > lat_r = lat*rad; > fc = 360*rad; % 360 degrees in radians > y = 365; %year in days results = zeros(365*24,1); % preallocate the results array idx = 0; % an index for the results > for J = 1:365; > decl = 23.44 * rad * sin(fc*((J+284)/y)); > for hr = 1:24; idx = idx+1; % increment it > HA = (12-hr)*pi/12; > HA_rad = HA * (pi/180); z = acos(sin(decl)*sin(lat_r)+ cos(lat_r)*cos(decl)*cos(HA_rad)); > results(idx) = 1376*(1-.48*1)*cos(z)*(.3+.73*cos(z)); > end > end > > I've tried save -append function but it didn't work for me. ... That would write stuff to disk and if in the right place should do so but if you want/need the whole schmear at once for later calc's this will do it. --
From: TideMan on 15 Jul 2010 21:15
On Jul 16, 11:20 am, "Maciej O" <meuv...(a)gmail.com> wrote: > Dear all, > > I'm very new to Matlab and struggling with completing my code for shortwave radiation. > I'm not sure how to record/save/keep the data (for further calculations) from within the loop before it gets overwritten during the subsequent loop. > Basically, the result should consists of 8760 (365*24) entries; one yearly oscillation (365 days) composed of daily (24hr) oscillations > > here is a simplified running version of the code: > > lat = -77.024722; > rad = 1*(pi/180); %degrees to radians > lat_r = lat*rad; > fc = 360*rad; % 360 degrees in radians > y = 365; %year in days > > for J = 1:365; > decl = 23.44 * rad * sin(fc*((J+284)/y)); > for hr = 1:24; > HA = (12-(hr))*pi/12; > HA_rad = HA * (pi/180); > z = acos(sin(decl)*sin(lat_r)+ cos(lat_r)*cos(decl)*cos(HA_rad)); > results (hr) = 1376*(1-.48*1)*cos(z)*(.3+.73*cos(z)); > end > end > > I've tried save -append function but it didn't work for me. Maybe it's just my syntax. I'm sure this is simple but for some reason I just can't make it happen. Thanks. You need to define results as a matrix like this: results=zeros(24,365); % this is called preallocation Put this before your first for loop. Then in your inner loop replace results(hr) with results(hr,J) Now, you have a matrix of results, with a day in each column, consisting of 24 rows (the hours). |