Unexpected date when ploting a timeseries MATLAB - matlab

I am trying to plot a timeseries using a cell array of strings representing an hour of measurements with a sample every 10 seconds. Below is the code I use to plot this data:
Voltages=[230.1,235.1,.......237];
Time={'13:00:10','13:00:20', '13:00:30'........'14:00:00'};
t=timeseries(Voltages, Time); % Using timeseries function in MATLAB
plot(t);
I also add two straight lines showing upper and lower voltage limits, and here is the chart I get:
As you can see in final result an unexpected date is shown on the x-axis ...
I want to do one of the following:
Remove the date altogether from the x-axis.
Using a date string 06.05.2015 I have in a variable, add this date instead of the 1-Jan-2015 unexpected date.
Remove the unexpected date from x-axis and then add the 06.05.2015 in text box.

From the documentation of timeseries class:
ts = timeseries(data,time) creates the time-series object using the specified data and time.
Where time above is a "time vector" as defined further down on the same documentation page:
Time Vector
A time vector of a timeseries object can be either numerical (double) values or valid MATLAB date strings.
When the timeseries TimeInfo.StartDate property is empty, the numerical time values are measured relative to 0 (or another numerical value) in specified units. In this case, the time vector is described as relative (that is, it contains time values that are not associated with a specific start date).
Before plotting the timeseries, try setting the TimeInfo.StartDate to what you need it to be, as shown in the example below:
Time = ['13:00:10';'13:00:20';'13:00:30'];
Data = [1 2.5 3];
start_date = '06.05.2015'; %// What you specified
ts = timeseries(Data,Time);
%// Since a date format separated with dots isn't supported in MATLAB, we replace . => /
ts.TimeInfo.StartDate = strrep(start_date,'.','/');
plot(ts)
Which results in:
                   

Related

How to define date column and plot that date column as x axis in R studio

I have half-hourly time series data; 48 data points in a day. The total time period starts from 4/6/2018 (i.e day/month/year) to 2/12/2018. I want to create a time series object. I am using ts function.
However, I am unable to define the frequency. As I know, frequency=1 for yearly data, but unable to define in my half-hourly data. How to solve this problem using ts object or there is any other way to do it?
After creating a time series object (i.e. date column), I want to plot it with the corresponding data series.
How to plot my result (in R object) on Y-axis and time period on the X-axis?
R <- function(x){
return(FDWhittle(x, method="discrete", sdf.method="multitaper"))
}
plot(R[,i],type="l",col = "Black", xlab="Time",
ylab="Return",main=names(R)[,i])
When I am plotting by using the above code, the results in the object "R" is coming on the Y-axis, and some values like (100, 200, 300--------) are coming on the X-axis. I want to get the time period on my X axis, but unable to define the intra-day date column having 48 data points in a day.

Function which finds temperatures for a given month from data

I'm having some issues creating a function with the following parameters:
Ndata = extperiod(data, year, month,time)
The data is a table with 3 columns, which from left to right are:
year/month/date, time, temperature
My goal is to create a function which can extract a time and a year/month, irrespective of the date and find it's corresponding temperature.
I need to avoid using for loops
I've been advised to use floor and find, where floor(YYYYMMDD/100) = YYYY*100 + MM, which I somehow want to integrate to my function.
I've previously found a way to extract all temperatures from the data for a given day, as follows:
k = find(data(:,1)==19750101);
data(k(1):k(end),3)
I'm trying to incorporate this method, but I think that the hint "floor(YYYYMMDD/100)" throws me a of a little.
I have tried with find(data(:,1)==floor(YYYYMMDD/100)), where I would think that I'd be given all dates with a specific year and month. For example:
find( data(:,1) == floor(19660101/100) )
I thought this would give me all points in the column vector where the value is 196601. But it doesn't.
What could I try differently?
From your explanation, your want to get all temperature for a given month, no matter time and day.
So you want to find dates that are comprised in the range [YYYYMM ; YYYY{MM+1}[ or [YYYYMM ; {YYYY+1}01[ in the case of selecting December.
Recall that you store the complete date in your table. So you need to apply your operator floor to both sides of your query, not only on the query value, because no date is floor(YYYYMMDD/100)!
As a result, try the following:
find( floor(data(:,1)/100) == floor(19660101/100) )

How do I take an n-day average of data in Matlab to match another time series?

I have daily time series data and I want to calculate 5-day averages of that data while also retrieving the corresponding start date for each of the 5-day averages. For example:
x = [732099 732100 732101 732102 732103 732104 732105 732106 732107 732108];
y= [1 5 3 4 6 2 3 5 6 8];
Where x and y are actually size 92x1.
Firstly, how do I compute the 5-day mean when this time series data is not divisible by 5? Ultimately, I want to compute the 'jumping mean', where the average is not computed continuously (e.g., June 1-5, June 6-10, and so on).
I've tried doing the following:
Pentad_avg = mean(reshape(y(1:90),5,[]))'; %manually adjusted to be divisible by 5
Pentad_dt = x(1:5:90); %select every 5th day for time
However, Pentad_dt gives me dates 01-Jun-2004 and 06-Jun-2004 as output. And, that brings me to my second point.
I am looking to find 5-day averages for x and y that correspond to 5-day averages of another time series. This second time series has 5-day averaged data starting from 15-Jun-2004 until 29-Aug-2004 (instead of starting at 01-Jun-2004). Ultimately, how do I align the dates and 5-day averages between these two time series?
Synchronization between two time series can be accomplished using the timeseries object. Placing your data into an object allows Matlab to intelligently process it. The most useful thing is adds for your usage is the synchronize method.
You'll want to make sure to properly set the time vector on each of the timeseries objects.
An example of what this might look like is as follows:
ts1 = timeseries(y,datestr(x));
ts2 = timeseries(OtherData,OtherTimes);
[ts1 ts2] = synchronize(ts1,ts2,'Uniform','Interval',5);
This should return to you each timeseries aligned to be with the same times. You could also specify a specific time vector to align a timeseries to using the resample method.

How do I plot on matlab with dates along the x-axis

I have two vectors(?) of data - one being prices, and the other the dates that those prices occured, and I am trying to plot a scatter plot of the two.
My dates are of the format ddmmyyyy and I have tried using mat2str to convert the vector into strings, and then used
formatin='ddmmyyy';
datenum(MYDATA,formatin)
however it returns an error saying that datenum has failed.
EDIT
This is the example of my code
This is what I am trying to run, where AvivaDate is a vector of 1200x1 double. The problem seems to be that mat2str is not changing the vector into a string of numbers: e.g. I need it in {'12345','12345'} form but mat2str is changing it into a string of '[12345' 12345]', so not a list of separate strings if that makes sense
formatin = 'ddmmyyyy';
DateAviva = mat2str(AvivaDate);
datenum(DateAviva,formatin);
hist(ReturnAviva,datenum(AvivaDate,formatin));
datetick('x','keepticks','keeplimits');
mat2str is not the function you want to convert between numbers and strings. mat2str has a very specific function (you see how it puts the [] around the output?). Use num2str, then convert it into a cell array:
S = num2str(AvivaDate); % should be 1200 x 8 char
C = mat2cell(S,ones(size(S,1),1)); % should be 1200 x 1 cell
dates = datenum(C,'ddmmyyyy'); % should be 1200 x 1 datenums
Although, depending where you're getting the information from in the first place, there may be a better way of reading the dates in from file, so you don't end up with a matrix of numbers where you want dates in the first place.

labeling x-axis with cell array

I have been searching various forums for hours but it seems impossible to do a thing in Matlab that's automatic in excel...
I used uiimport to import an xls file with into two arrays (? total newbie), one containing dates for my x-axis and the other the values I want to plot.
I have 180 values. The dates are three dates per month, more or less ranging from May 2008 until now, end of March.
Using
plot(mynumbers)
set(gca,'XTickLabel',dates)
only puts dates for May 2008 on my x-axis!
where did all the other dates go?
Instead using
plot(mynumbers)
set(gca,'XTick',mynumbers,'XTickLabel',dates)
gives error message
"??? Error using ==> set
Values must be monotonically increasing."
Please help!
where did all the other dates go?
The answer to your first question is that MATLAB only uses the first N number of strings corresponding to the default N number of tick marks on the x axis.
"??? Error using ==> set Values must be monotonically increasing."
The error is telling you that your date ticks must be evenly spaced. Instead of using dates corresponding to your actual data points, you could grab the x tick values that MATLAB automatically assigned to your graph, translate them to text, and then reassign the dates as x tick labels, like so:
% generate example unevenly spaced date vector
time = [now,now+1,now+25,now+28.5,now+36,now+40,now+51,now+65];
% generate random data points
data = rand(size(time));
% plot time vs data, storing the axes handle in the process
figure;
axH = axes;
plot(axH,time,data)
% get the x-axis tick locations
ticLoc = get(axH,'XTick');
% format tick labels (substitute any date format you wish)
ticLab = cellfun(#(x) datestr(x,'mm/dd'),num2cell(ticLoc),'UniformOutput',false);
% apply tick labels
set(axH,'XTickLabel',ticLab)
MATLAB's built-in function datetick also performs similarly.
However, if you zoom afterwards, you won't have accurate tick labels. So you may want to use datetick2 on the File Exchange.
If you're having trouble converting a cell array of dates from Excel into a numeric array, use:
dateNumeric = cell2mat(cellfun(#datenum,dateStrings,'UniformOutput',false));
try set (gca,'XTickLabel',num2str(dates))