Showing date and time in matlab plot [closed] - matlab

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I have the following data:
x: minutes over 2 months (so x has a length of 2*60*24*60)
y: numerical data corresponding to each minute of a day over 2 months
When I plot data, I want to show on the x-axis the dates in the format of month-day and when plot is zoomed the hour and minute of each day to be displayed.
How can I do this?

You can use datetick to do that. When you do not specify a format, MATLAB automatically adjusts the scale when zooming.
If your date aren't in datenum format, change those first, e.g.
% Convert your starting time to a datevec
date_start = datevec('1995/03/21 12:38:15');
% Extend for all required times
date_mat = repmat(date_start, numel(x), 1);
% Add a minute to each row
date_mat (:, 5) = date_mat (:,5) + (0:numel(x)-1).';
datenum_array = datenum(date_mat);

Related

Linear Interpolation in MATLAB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two arrays the first one represents the time axis with time stamp 1 min
time=[0,60,60,120,180,240,300,360,420,480,540]
and the second array represents a data values as follows
data=[18,12,12,0,7,9,6,8,12,18,0]
what im trying to do is two things:
1-I would like to fix the time axis to have 1 second time stamp
2-Perform linear Interpolation as follows:
for example i have
enter image description here
and i would like to have sth like this:
enter image description here
In case of time repetation like the repated 60 seconds the duplication should be deleted
You can remove duplicates (the first value is kept) with
time = [0,60,60,120,180,240,300,360,420,480,540];
data = [18,12,12,0,7,9,6,8,12,18,0];
[time_u unique_indeces] = unique(time);
data_u = data(unique_indeces);
clear unique_indeces;
and interpolate with
time_i = linspace(min(time), max(time), max(time) - min(time) + 1);
data_i = interp1(time_u, data_u, time_i);
I prefer linspace because I usually want to set the number of data points and not the space between points but you can also use min(time):max(time) or time(1):time(end) instead.
This code will also sort your data points by time.
Function interp1 does the job:
time=[0,60,120,180,240,300,360,420,480,540];
data=[18,15,0,7,9,6,8,12,18,0];
time_1s = 0:540;
data_interpd = interp1(time, data, time_1s);
Note: I have manually deleted the first duplicate value at time 60. If there is only one value to remove (always at same place), I think the best is to remove it by using a mask, since unique removes the second occurrence of duplicates and not first one.

MATLAB: How to sort .txt data file with 1000 data points (10 collected each day for 100 days)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I loaded in the 1000x1 .txt file but I need to make it 100x10? How do I do this in matlab?
Thanks
Reshape should do this for you. https://www.mathworks.com/help/matlab/ref/reshape.html
But you haven't explained any criteria regarding what the change of dimensions is based on. So, it may not be exactly what you want.
You can use the reshape function to do this. In the example below I created a column vector x that is 1000 x 1 containing numbers that ramp from 1 to 1000. I didn't know what order you wanted the rows and columns populated, so I created variables x2 and x3 with the two variations, you can choose the form that fits your needs.
x = (1:1000)';
% x2 is created with one column at a time
x2 = reshape(x, 100, 10);
% x3 is created one row at a time
x3 = reshape(x, 10, 100)';

How to make a new matrix based on column values in an old matrix? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am very new to Matlab and coding in general, so I apologize if this is a basic question.
I have a matrix of three columns (data1) where the first column refers to time (s).
I would like to make a new matrix (bout1) consisting of entire rows of the matrix data1 based on the values in the first column (so, for example, in a range from 30 s to 120 s).
I know how to extract the rows based on the row number:
bout1 = data1(361126:391643,:)
but not based on the values in a specific column.
You can use the find function (see here) to find the rows you need, like this:
time = data1(:, 1);
i = find(30 <= time & time <= 120);
bout1 = data1(i, :);

Creating an axis of months [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I would like to create an axis from the dates 03-01-2014 to 2-28-2015 with (289*12) individual data points. So for each month the X-axis will have the month shown for every 288 data points I have in my array.
Does someone know how I can achieve this?
You can do this by three function which already available in Matlab toolbox
first plot your data(it would be better if you say how we can regenerate your data because of that i try my own )
x = [1 2 3 4 5 6];
data = sin(x);
plot(x,data)
this 3 lines code gave me below plot for instance.
so how we could add month to axis ?! we can do it with set and gca like below
set(gca,'xtick',1:6); % this xtick is very important(i said in this line i want to have x axis ticks in 1,2,3,..,6 so if your x axis is different you must change this line)
set(gca,'xticklabel',{'03-01-2011','03-01-2012','03-01-2013','03-01-2014','03-01-2015','03-01-2016'})
set(gca,'XTickLabelRotation',30)
the output will be

How to create a dates series that gives me only the end of the month days? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
How do I create a dates series that gives me only the end of the month days?
i.e.
31/01/1993
28/02/1993
...
31/01/2013
The easy way would be to use the start of month days and subtract one. So, for instance:
datevec(datenum(1993,2,1)-1);
This would give you 1993 01 31. It is fairly simple to then do the same for each following month. e.g.
datenum(1993,2:13,1)-1;
would give you the end of month days for the whole of 1993.
EDIT:
As Luis pointed out, if you want the dates as formatted strings, as in the original question, you would use datestr:
% The '24' causes datestr to use a predefined format of the form 'dd/mm/yyyy'
datestr(datenum(1993,2:13,1)-1,24)
EDIT2:
To do multiple years, you can just increase the number of months. MATLAB's datenum function is clever - if you give it a date datenum(1993,14,1) it will return 1994/2/1, so if you wanted every month from 1993-2013 (20 years, so 240 months) you would use
datestr(datenum(1993,2:241,1)-1,24)
Use calendar function:
for year = 1993:2013
for month = 1:12
lastday = max(max(calendar(year,month)));
fprintf('%02d/%02d/%04d\n',lastday,month,year)
end
end
You can of course store result in matrix (or cell array) by changing fprintf with sprintf:
dates = char(zeros(12*21,10));
idx = 0;
for year = 1993:2013
for month = 1:12
idx = idx + 1;
lastday = max(max(calendar(year,month)));
dates(idx,:) = sprintf('%02d/%02d/%04d',lastday,month,year);
end
end
---EDIT---
I've found thath there is aslo eomday function. So instead of line:
lastday = max(max(calendar(year,month)));
You can use:
lastday = eomday(year,month);
To see the algorithm of eomday function type in MATLAB:
open eomday