How to read files using for loop in Matlab? - matlab

I need to read 8760 files (365 days * 24 hours = 8760) of small size (60 kb) and aggregate values and take average of some values.
Earlier, I have used the below stated code for reading *.csv files:
for a=1:365
for b=1:24
s1=int2str(a);
s2=int2str(b);
s3=strcat('temperature_humidity',s1,'_'s2);
data = load(s3);
% Code for aggregation, etc
end
end
I was able to run this code. However now the file name is little different and I am not sure how to read these files.
Files are named like this:
2005_M01_D01_0000(UTC-0800)_L00_NOX_1HR_CONC.DAT
where M = Month, so the values are 01, 01, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12;
D = Day, so the values are 01, 02, 03, ..., 31;
Hours is in this format: 0000, 0100, 0200, ..., 1800, ..., 2300.
Please take a look at the attached image for file name . I need to read these files. Please help me.
Thank you very much.

I would use dir:
files=dir('*.dat')
Or you can construct the filenames with
name = sprintf('%d_M%2d etc.',...)

Related

How to calculate countdown timer with a single timezone for all users in dart?

I'm creating an auction page.As of now the code works fine and Its showing a countdown timer which only calculates the difference according to the local timezone but not all users will have the same timezone. So I tried .toUtc but it still calculates from the device local timezone.
Im using this package to calculate and display timer.
https://pub.dev/packages/flutter_countdown_timer
All these codes are from the package.
_diffTime(Duration duration) {
value = max(value - duration.inMilliseconds, 0);
_lastTimestamp = DateTime.now().toUtc().millisecond;
if (value <= 0) {
stop();
onEnd?.call();
return;
}
}
This is the input for the timer.
CountdownTimer(
endTime: DateTime(2020, 10, 22, 12, 48, 00).toUtc().millisecondsSinceEpoch,
textStyle: TextStyle(fontSize: 30, color: Colors.pink),
)
Your CountdownTimer object hard-codes the end time to be DateTime(2020, 10, 22, 12, 48, 00).toUtc(). This creates a DateTime object representing October 22, 2020 12:48:00 PM in your local time and then returns the corresponding UTC time. Your hard-coded end-time therefore would represent different points in time when your code is run on systems using different local time zones.
If you instead want October 22, 2020 12:48:00 PM as a UTC time so that it represents the same moment in time regardless of the system's local time zone, then you should construct it as DateTime.utc(2020, 10, 22, 12, 48, 00). DateTime.utc(...) and DateTime(...).toUtc(...) are not the same thing.
Also, even though your _diffTime function is completely irrelevant to your question, I'll point out that:
_lastTimestamp = DateTime.now().toUtc().millisecond;
doesn't do what you probably intend. DateTime.now().toUtc().millisecond returns the millisecond component of the current DateTime. You likely instead intended to use DateTime.now().millisecondsSinceEpoch. Note that .toUtc() is unnecessary.

Extract date from string with another numbers from R

I need to extract the date from this text:
Mellisoni 2014 Malbec (Columbia Valley (WA))
Okapi 2013 Estate Cabernet Sauvignon (Napa Valley)
Podere dal Nespoli 2015 Prugneto Sangiovese (Romagna)
Simonnet-Febvre 2015 Chablis
Lagler 2012 1000 Eimerberg Smaragd Neuburger (Wachau)
I use this code:
vino<-mutate(vino, year1=sub("^.*([0-9]{4}).*", "\\1", vino$title))
It works, but I have the last value extract on 1000 instead of 2012, how can I fix it if have another numbers?

Chosing specific dates/hours from an array

I have a matrix that has 3months of data or so..Its a 952x1 matrix with the elements in the following format(3 hourly )
Aug-05-2015 03:00:00
Aug-05-2015 06:00:00
Aug-05-2015 09:00:00
Aug-05-2015 12:00:00
Aug-05-2015 15:00:00
Aug-05-2015 18:00:00
Aug-05-2015 21:00:00
Aug-06-2015 00:00:00
Aug-06-2015 03:00:00
Aug-06-2015 06:00:00
I would want to choose say only day timings/ only night or say for august month alone. How do i do that.
Further to my question, if I have a group of .wav files and Im trying to pick only month wise or do daily psd averages etc or chose files belonging to a month how to go about? The following are first 10 .wav files in a .txt file that are read into matlab code-
AMAR168.1.20150823T200235Z.wav
AMAR168.1.20150823T201040Z.wav
AMAR168.1.20150823T201845Z.wav
AMAR168.1.20150823T202650Z.wav
AMAR168.1.20150823T203455Z.wav
AMAR168.1.20150823T204300Z.wav
AMAR168.1.20150823T205105Z.wav
AMAR168.1.20150823T205910Z.wav
AMAR168.1.20150823T210715Z.wav
yyyymmddTHHMMSSZ.wav is part of the format to get sense of some parameters.
Thanks.
Are these datetimes? If so, you can use logical indexing here if you make use of some of the datetime functions. To get the times in August:
t = datetime(2015, 8, 1, 3, 0, 0) + hours(3:3:3000)';
t(month(t) == 8) % Times in August
To get the times that are during the day or night:
t(hour(t) < 12) % Day times
t(hour(t) >= 12) % Night times

UNIX: Convert Unix Date in Specific format

I have some date other than current date in unix and I want to convert into a specific format
Original Format
D="Mon Dec 30 06:35:02 EST 2013"
New Format
E=20131230063502
E=`date +%Y%m%d%H%M%S`
this is the way to format the output of the date command and save it in the variable E
Using python:
def data(dstr):
m = {'Jan': '01', 'Feb':'02', 'Mar':'03', 'Apr':'04', 'May':'05', 'Jun':'06', 'Jul':'07', 'Aug':'08', 'Sep':'09', 'Oct':'10', 'Nov':'11', 'Dec':'12'}
val = dstr.split(' ')
month = m[val[1]]
time = val[3].split(':')
return '{}{}{}{}{}{}'.format(val[-1],month,val[2],time[0],time[1],time[2])
if __name__ == '__main__':
print data("Mon Dec 30 06:35:02 EST 2013")
In: Mon Dec 30 06:35:02 EST 2013
Out: 20131230063502

MATLAB Cut 3D array of daily data into monthly segments

I have a 1437x159x1253 large matrix (let's call it A) of daily sea ice data for a little over 2 years. I need to write a code that takes the daily data from each month and does mean(A, 3) on it. So basically, 1253 is the t in days. If I start from January, I need to do mean(A,3) of the first 31 days, then the mean(A,3) of February, the next 28 or 29 days. Because the days alternate between 31 and 30 (and 28 or 29 for February), I don't know how to write a code to do this. I can do it manually, but that would take a while.
Thanks!
You can initialize an array containing the number of days in each month, Mon = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] using boolean to check whether it's a leap year (to set Mon(2) = 29). The number of days will help you index each month appropriately, using a loop like:
index=1;
for i=1:12
average = mean(A(:,:,index:(index+Mon(i)-1),3);
index = index+M(i); % Starting location of the next month
end