formatting character arrays [duplicate] - string

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Counting values by day/hour with timeseries in MATLAB
This is an elementary question, but I cannot find it:
I have a 3000x25 character array:
2000-01-01T00:01:01+00:00
2000-01-01T00:01:02+00:00
2000-01-01T00:01:03+00:00
2000-01-01T00:01:04+00:00
These are obviously times. I want to reformat the array to be a 3000x1 array. How can I redefine each row to be one entry in an array?
(Again, this is simple, I'm sorry)

Other than converting to serial date numbers as other have shown, I think you simply wanted to convert to cell array of strings:
A = cellstr(c)
where c is the 3000x25 matrix of characters.

You need to specify a format for the array and feed it to datenum, like this:
>> d = datenum(c,'YYYY-MM-DDTHH:mm:ss')
d =
1.0e+005 *
7.3487
7.3487
7.3487
7.3487
The times are now stored as datenums, i.e. as floating point numbers representing the number of days elapsed since the start of the Matlab epoch. If you want to convert these to numbers representing the fraction of the day elapsed, you can do
>> t = d - fix(d);
and if you want the number of seconds since midnight, you can do
>> t = 86400 * (d - fix(d));
t =
61.0000
62.0000
63.0000
64.0000

Related

how to get zulu time difference seconds/milliseconds in excel

The excel column contains Zulu Timzezones How to calculate the difference between two dates in seconds.milliseconds
From Time 2022-04-25T04:16:57.823121842Z
To Time
2022-04-25T04:16:58.173194593Z
2022-04-25T04:16:58.089133751Z
2022-04-25T04:16:58.462278784Z
2022-04-25T04:16:57.829376293Z
2022-04-25T04:16:57.961790312Z
2022-04-25T04:16:58.445884586Z
2022-04-25T04:16:57.830806273Z
2022-04-25T04:16:58.067723338Z
2022-04-25T04:16:58.470913276Z
2022-04-25T04:16:57.838787068Z
When I Try to Do something like =B13-B14
Error
Function MINUS parameter 1 expects number values. But '2022-04-25T04:35:59.092943356Z' is a text and cannot be coerced to a number.
Converted to Number format
REVISED: I forgot to convert the milliseconds
You can convert the date strings into time values by breaking them into parts:
=DATEVALUE(LEFT(A2,10)) + TIMEVALUE( MID(A2,12,8) ) --MID(A2,20,10)/24/60/60
Where A2 is the date string.
This assumes that they have the exact structure that you have shown and fully padded with zeros. If that is not the case, for example the milliseconds could be .095Z, then you can mod this to:
=DATEVALUE(LEFT(A2,10)) + TIMEVALUE( MID(A2,12,8) ) --MID(SUBSTITUTE(A2,"Z",""),20,999)/24/60/60
to be safe.

How to get to the first 4 numbers of an int number ? and also the 5th and 6th numbers for example

I have a function that checks if a date ( int number ) that is written in this format: "YYYYMMDD" is valid or not.
My question is how do i get to the first 4 numbers for example ( the year )?
the month ( the 5th and 6th number ) and the days.
Thanks
Probably the easiest way would be to convert it to a string and use substrings or regular expressions. If you need performance, use a combination of modulo and division by powers of 10 to extract the desired parts.
There is a simple way converting to string then slicing it:
Example for year:
date = str(date)
year = date[0:3]
for your example using the following format "YYYYMMDD"

append cell row to matrix

I'm reading an excelfile in matlab with
[NUM,TXT,RAW]=xlsread(DATENEXCEL,sSheet_Data);
In the excelfile are different datamatrices in different sheets in the following form
Date Firm1 Firm2 Firm3 ...
1.1.16 12 12 12
... ... ... ...
Currently I'm handling the pure data with the NUM object and the header row with the TXT object. My first issue is how to combine the header row with the data rows. Looping does not work, since I predefine the data matrix with
daten=zeros([length(sDatesequence) size(RAW,2)]);
because I want to be able to add more data from different sources to that object. Predefining with zeros, however, leads Matlab to expect doubles and not characters. Converting the cell array TXT with cell2mat delivers unsatisfying results:
cell2mat(TXT(1,:))=Firm1Firm2Firm3...
hence only a long string vector.
Question: Is there another way to combine character vectors and double matrices?
Regards,
Richard
You can combine them in a cell array.
c{1,1} = 'Firm1';
c{1,2} = datavector;
c{2,1} = 'Firm2';
c{2,2} = datavector;
But as far as I know it is not possible to add text headers to a numerical matrix, unless you do something with typcasting. But I would not recommend that.
d(1:8)='Firm1 '; %must have exactly eight characters (a double has a length of 8 bytes)
y = typecast(uint8(d),'double') %now you have a number that would fit in a matrix of doubles
x=char(typecast(y,'uint8')) %now it's converted back to text

Convert time string from unix time command like 10m20.5s into time format in excel

I have time data from the unix time command like
203m53.5s
I have this data in excel. I want it to be converted to Excell time format so I can do mathematical operations like sum and averages over them.
How can I do this
Replace the m with : and the s with "":
=--SUBSTITUTE(SUBSTITUTE(A1,"m",":"),"s","")
Now that the time is in a format that Excel will recognize we need to change it from string text to a number. The -- is forcing the string into a number by performing a mathematical process of multiplying -1 * -1 to it.
It can be replaced by TIMEVALUE()
Then format the cell with a custom format of:
[mm]:ss.0
One way is to use a forumala to strip out the m and s and use those values for time in a new column in Excel.
Assume the Unix data is in column A.
=(LEFT($A1,FIND("m",$A1)-1)*60+MID($A1,FIND("m",$A1)+1, LEN($A1)-FIND("m",$A1)-1)/84600
then format the cell as custom and choose the time without the AM/PM
Breakdown:
(get the minutes by finding "m")
multiply by 60 to convert to seconds
+ (get the seconds by starting at the location of m, +1 to the location of m-length of the whole string)
-1 to account for the actual "s"
Then divide the whole thing by 84600 to convert to time as a decimal

Dealing with date strings with different formats in Matlab

I have a very long vector of date strings (1000000+) in Matlab that I want to convert to a serial number format. The issue is that not every date string is the same format. They are one of two formats,
'2010-03-04 12:00:00.1'
or
'2010-03-04 12:00:00'
The problem is that not all the strings have the millisecond precision. There is no regular pattern as to where these strings without milliseconds occur. The data is originally read from a data file, and the strings currently exist as cell arrays. My work around this is as follows:
for i=1:length(dates),
if length(dates{i})==19
dates(i)=datenum(temp);
elseif length(dates{i})==21
dates(i)=datenum(temp,'yyyy-mm-dd HH:MM:SS.FFF');
end
end
Is there perhaps a better way to go about this? It is important that I retain the millisecond precision when it is present. The intent of this is that I will have to extract and calculate statistics on data associated with each time based on different time criteria, and I figured it would be easier if the dates were handled as numbers.
In MATLAB R2010b, I'm able to get the desired output when calling DATENUM with no additional formatting arguments:
>> dateStrs = {'2010-03-04 12:00:00.1'; ... %# Sample strings
'2010-03-04 12:00:00'};
>> datenum(dateStrs)
ans =
1.0e+005 *
7.3420 %# The same? No, the Command Window just isn't displaying
7.3420 %# many places after the decimal point.
>> format long %# Let's make it show more decimal places
>> datenum(dateStrs)
ans =
1.0e+005 *
7.342015000011574 %# And there's the difference!
7.342015000000000

Resources