Reading CSV file with text using textread - matlab

I am reading a csv file in Matlab using textread function and storing the values in the cells of string and float types.
[string1, string2, values] = textread('/path/xyz.csv', '%s %s %f', 'headerlines', 1);
Data has three columns. Two of them I believe are of string type and one is float.
Sample Data
#timestamp host value
March 5th 2019, 13:41:54.879 tscompute1 0.399
March 5th 2019, 13:41:54.879 tscompute1 0.599
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 0.2
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 100
March 5th 2019, 13:41:54.879 tscompute1 0.4
There is not execution error. But the read values are not as expected. Please find the sample output below.
Values stored in string1 looks like as follows
'"March'
','
'"March'
','
'"March'
','
'"March'
','
Values stored in string2 looks like as follows
'5th'
'13:41:54.879",tscompute1,0.399'
'5th'
'13:41:54.879",tscompute1,0.599'
'5th'
'13:41:54.879",tscompute1,0'
'5th'
'13:41:54.879",tscompute1,0.2'
Values stored in values looks like as follows
2019
0
2019
0
2019
0
2019
0

Your text seems to have inconsistent delimiters, the date is separated from the time by a comma, while the time, the name "tscompute1" and the number are separated by white-spaces.
The simplest is to read every line as six elements each separated by white-spaces with five of them being strings and the sixth being a number.
[s1, s2, s3, s4, s5, values] = textread('/path/xyz.csv', '%s %s %s %s %s %f', 'headerlines', 1);
That allows you to get the date (concatenate strings in s1-s3, remove the trailing comma), the time (s4), the name (s5) and the value.

Related

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?

parse javascript date to elixir format

I have some saved dates in JavaScript using new Date() that looks like:
"Sun Feb 24 2019 14:44:20 GMT+0200 (Eastern European Standard Time)"
I'm trying to parse these to Elixir DateTime; I didn't find anything in "timex" that can help and I already know that I can use DateTime.from_iso8601 but for dates saved using new Date().toISOString() but what i need is to parse the above string.
Thanks in advance
You can use elixir binary pattern matching to extract the date parts and parse using Timex's RFC1123 format. The RFC1123 is the format e.g Tue, 05 Mar 2013 23:25:19 +0200. Run h Timex.Format.DateTime.Formatters.Default in iex to see other formats.
iex> date_string = "Sun Feb 24 2019 14:44:20 GMT+0200 (Eastern European Standard Time)"
iex> <<day_name::binary-3,_,month_name::binary-3,_,day::binary-2,_,year::binary-4,_,time::binary-8,_::binary-4,offset::binary-5,_,rest::binary>> = date_string
iex> Timex.parse("#{day_name}, #{day} #{month_name} #{year} #{time} #{offset}", "{RFC1123}")
iex> {:ok, #DateTime<2019-02-24 14:44:20+02:00 +02 Etc/GMT-2>}
Pattern matching:
The binary-size are in byte sizes. 1 byte == 1 character. For instance to get
3-character day_name the size is 3. Underscores (_) is used to pattern match the spaces in the date format
Updated answer to use binary-size rather than bitstring-size for simplicity
I didn't find anything in "timex" that can help
The Timex Parsing docs say that you can use strftime sequences, e.g %H:%M:%S, for parsing. Here's a list of strftime characters and what they match.
Here's a format string that I think should work on javascript Dates:
def parse_js_date() do
Timex.parse!("Sun Feb 24 2019 14:44:20 GMT+0200 (Eastern European Standard Time)",
"%a %b %d %Y %H:%M:%S GMT%z (%Z)",
:strftime)
end
Unfortunately, %Z doesn't want to match the time zone name, which causes Timex.parse!() to spit out an error. It looks like %Z in Elixir only matches one word, e.g. a timezone abbreviation EET. Therefore, my simple, clean solution is spoiled.
What you can do is chop off the time zone name before parsing the date string:
def parse_js_date_string() do
[date_str|_tz_name] = String.split(
"Sun Feb 24 2019 14:44:20 GMT+0200 (Eastern European Standard Time)",
" (",
parts: 2
)
Timex.parse!(date_str,
"%a %b %d %Y %H:%M:%S GMT%z",
:strftime)
end
In iex:
~/elixir_programs/my$ iex -S mix
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Compiling 1 file (.ex)
Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> My.parse_js_date_string()
#DateTime<2019-02-24 14:44:20+02:00 +02 Etc/GMT-2>
iex(2)>

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

How to detect 4 digit using regexp

How do I get the year (4 digits) when given a source code, I can only detect the day (29), but could not detect the year(1997). There is something wrong in my regexp checking.
age = regexp(CharData,'(\d{1,4})','match','once')
For example,
Registered On
March 29, 1997
Desired output: 1997
Error output: 29
for i = 1:2
data2=fopen(strcat('DATA\PRE-PROCESS_DATA\F22_TR\f22_TR_pdata_',int2str(i),''),'r')
CharData = fread(data2, '*char'); %read text file and store data in CharData
fclose(data2);
age = regexp(CharData,'(\d{4})','match','once')
end
file : f22_TR_pdata_1 --> Registered On
June 24, 1997
file : f22_TR_pdata_2 --> Registered On
March 29, 1997
Age: 1997
To only grab four digits
age = regexp(CharData,'(\d{4})','match','once')
Doing d{1,4} means look for numbers with a length between 1 and 4. Meaning, 1, 29, 123, 4444 would all match because their length is between 1 and 4
d{4} says, get me the number with exact length of 4. Meaning, 1997, 2001, 1800 would all match.

Powershell: get-date & [datetime]::FromFileTime returns different values

Why does get-date & [datetime]::FromFileTime returns different values when converting FileTime? An example:
Get-Date 129442497539436142
returns Thursday, March 10, 0411 4:55:53 PM, but
[datetime]::FromFileTime("129442497539436142")
returns Thursday, March 10, 2011 11:55:53 AM
They produce the same result for me, presumably because I'm in GMT.
(FromFileTime parses the time as UTC, Get-Date appears to be using your local time.)
FileTimes are so-called Ticks. 10 million pass every second. Filetime are 0 at midnight, January 1st 1601 (UTC).
Get-date also have ticks, but the base of ticks used by Get-Date is NOT 1601.
It is January 1st year 1.
You can basically identify the 2 different types of ticks by the first digit.
Filetime ticks starts with digit 1 in the rough range of -100 to + 200 years from now.
The ticks using base on January 1st year 0 starts with 6 in roughly the same time range...
In PowerShell you can get verify Jan. 1st year 1 is tick 0 by typing:
[datetime]'0001-01-01' | Select-Object -property Ticks
Get-Date get ticks from 01/01/0001 00:00
[datetime]::FromFileTimeUTC($a) get ticks from 01/01/1601 00:00
You wrote:
returns Thursday, March 10, 0411 4:55:53 PM, but
returns Thursday, March 10, 2011 11:55:53 AM
The difference is 1600 years
This is an example:
$a = ([datetime]::Now).Ticks - ([DateTime]("01/01/0001 00:00")).Ticks
Get-Date $a # get ticks from 01/01/0001 00:00
$a = ([datetime]::Now).Ticks - ([datetime]("01/01/1601 00:00")).Ticks
[datetime]::FromFileTimeUTC($a) # get ticks from 01/01/1601 00:00
It is based on your local "long date" format.
enter image description here
Either you have to change your system "Long Date"
(OR) Use below command
(get-date).tostring("dd/MM/yyyy")
enter image description here
https://technet.microsoft.com/en-us/library/ee692801.aspx
Regards,
Manikandan Boopathy