Matlab change x axis tick label - matlab

I am relatively inexperienced with matlab, as I only use it occasionally. I am trying to plot a large range of values against time and I am running into some problems.
The data, which is from a text file, with about 55000 entries, gives the information in the following format:
year month day hour minute second value
The seconds column has accuracy of 6 decimal places and there are about 24hrs worth of data.
What I want to do is plot the values against time, which works fine. However as a result of my code below, the x-axis has label ticks in serial date number format, which is not very useful when looking at the figure. I want to change the labels to something more useful such intervals of hours. However I am not sure how to go about doing this.
Here is the code:
A = dlmread('data.txt',' ');
time = datenum(A(:,1),A(:,2),A(:,3),A(:,4),A(:,5),A(:,6));
scatter(time,A(:,7),1)
axis([min(time) max(time) min(A(:,7)) max(A(:,7))])
I found a solution here: matlab ticks with certain labels however, the process here is manual and with so much information I don't want to do this manually. How would I automate this process? or is there a better way to do what I am trying to achieve?
EDIT: I also found this method: http://www.mathworks.com/help/matlab/ref/datetick.html#btpnuk4-1, however, I dont want to show the actual date, I rather want to show intervals of time, ie an hour or 30 minutes.
EDIT 2: I have found a somewhat satisfactory solution. It could still be improved upon, so I don't know if I should submit this as an answer to my own question or not, but here it is:
A = dlmread('data.txt',' ');
time = datenum(A(:,1),A(:,2),A(:,3),A(:,4),A(:,5),A(:,6));
temp= time(1);
timediff = time - temp;
scatter(timediff,A(:,7),1)
axis([min(timediff) max(timediff) min(A(:,7)) max(A(:,7))])
datetick('x', 'HH')
This takes the original time vector in serialized time format and subtracts the first time from all the subsequent times to get the difference. The it uses the datetick function to to convert that to hours. It isn't ideal because instead of 24 hours it goes back to 00, but its the best I have tried thus far.

With reference to the other article, you will have to follow the same method but in order to automate the process you'll need to form the vectors of xtick and xticklabels as you read in the data and after you've plotted the data change the xticks and xticklabels.
Its not difficult what you're trying to do, but I will need more details of how you want to organize the ticks to be able to exactly say the steps that you'd have to follow

Matlab serial time is simply days since January 1, 0000, so your timediff variable is really elapsed days (and fractions thereof) since the start of your experiment. If you want your x ticks to be elapsed hours you could multiply timediff by 24.
scatter(timediff * 24, values)
This avoids the weirdness that can arise when using datetick as well.

Related

Matlab average number of customers during a single day

I'm having problems creating a graph of the average number of people inside a 24h shopping complex. I have two columns of data on a spreadsheet of the times a customer comes in (intime) and when he leaves (outtime). The data spans a couple of years and is in datetime format (dd-mm-yyyy hh:mm:ss).
I want to make a graph of the data with time of day as x-axis, and average number of people as y-axis. So the graph would display the average number of people inside during the day.
Problems arise because the place is open 24h and the timespan of data is years. Also customer intime & outtime might be on different days.
Example:
intime 2.1.2017 21:50
outtime 3.1.2017 8:31
Any idea how to display the data easily using Matlab?
Been on this for multiple hours without any progress...
Seems like you need to decide what defines a customer being in the shop during the day, is 1 min enough? is there a minimum time length under which you don't want to count it as a visit?
In the former case you shouldn't be concerned with the hours at all, and just count it as 1 entry if the entry and exit are in the same day or as 2 different entries if not.
It's been a couple of years since I coded actively in matlab and I don't have a handy IDE but if you add the code you got so far, I can fix it for you.
I think you need to start by just plotting the raw count of people in the complex at the given times. Once that is visualized it may help you determine how you want to define "average people per day" and how to go about calculating it. Does that mean average at a given time or total "ins" per day? Ex. 100 people enter the complex in a day ... but on average there are only 5 in the complex at a given time. Which stat is more important? Maybe you want both.
Here is an example of how to get the raw plot of # of people at any given time. I simulated your in & out time with random numbers.
inTime = cumsum(rand(100,1)); %They show up randomly
outTime = inTime + rand(100,1) + 0.25; % Stay for 0.25 to 1.25 hrs
inCount = ones(size(inTime)); %Add one for each entry
outCount = ones(size(outTime))*-1; %Subtract one for each exit.
allTime = [inTime; outTime]; %Stick them together.
allCount = [inCount; outCount];
[allTime, idx] = sort(allTime);%Sort the timestamps
allCount = allCount(idx); %Sort counts by the timestamps
allCount = cumsum(allCount); %total at any given time.
plot(allTime,allCount);%total at any given time.
Note that the x-values are not uniformly spaced.
IF you decide are more interested in total customers per day then you could just find the intTimes with in a given time range (each day) & probably just ignore the outTimes all together.

Remove Spikes from Periodic Data with MATLAB

I have some data which is time-stamped by a NMEA GPS string that I decode in order to obtain the single data point Year, Month, Day, etcetera.
The problem is is that in few occasions the GPS (probably due to some signal loss) goes boinks and it spits out very very wrong stuff. This generates spikes in the time-stamp data as you can see from the attached picture which plots the vector of Days as outputted by the GPS.
As you can see, the GPS data are generally well behaved, and the days go between 1 and 30/31 each month before falling back to 1 at the next month. In certain moments though, the GPS spits out a random day.
I tried all the standard MATLAB functions for despiking (such as medfilt1 and findpeaks), but either they are not suited to the task, either I do not know how to set them up properly.
My other idea was to loop over differences between adjacent elements, but the vector is so big that the computer cannot really handle it.
Is there any vectorized way to go down such a road and detect those spikes?
Thanks so much!
you need to filter your data using a simple low pass to get rid of the outliers:
windowSize = 5;
b = (1/windowSize)*ones(1,windowSize);
a = 1;
FILTERED_DATA = filter(b,a,YOUR_DATA);
just play a bit with the windowSize until you get the smoothness you want.

How to change the X-axis from milliseconds into hours in Matlab

I have some data measured every 500 milliseconds, you can see my data in the following image:
Now what I would like to do, is to convert the X-axis into hours instead of milliseconds. How can I do this in Matlab? How can do this generally if I want for example minutes? days? etc.
Thank you for your help =)
Well, that's pretty easy. Assume your vector is time_ms, to get it in hours, you just need:
time_h = time_ms/(1000*3600);
Then instead of doing something like:
plot(time_ms,my_data)
Just do:
plot(time_h,my_data)
If you have 100 data collected every 500 ms and you simply rescale the X axis to hours, you'll still get 100 data but as they had been collected every hour.
For example, suppose you have 3600 data collected every second.
data = rand(1,3600);
time_s = 1:3600;
Now, suppose you want to plot the data every minute. Without any processing, you could get every 60th sample in the data array.
data_m = data(1:60:3600);
time_m = 1:60;
Doing this way, you'll get a consistent plot.
(I'd attach images but I lack reputation)

Find time stamps in a given time interval (start/end) in a 2 column matrix

MatLAB noob here..
I have a 2 column matrix with start/end (in seconds) times - in a 2 column matrix.
I also have a single column matrix of time stamps. How do I find the time stamps that occur in each interval?
Not going to put any code here, as you provided none.
There are some alternatives, this is the most straight forward (though might not be the most efficient for the computer):
Use a for loop, for each line of your start/end matrix, and for each do another for loop for each element in your time stamp matrix, and assess, using if function, if each time stamp is between start and end times.
If you don't know how to use FOR and IF, type help FOR, help IF and find out. Or google it

matlab query in order to see how used the app is

I have a MySQL table with over 6 million records, each with a epoch timestamp. I need to plot all the timestamps across time of day. In other words, I need to see how many timestamps are between 7am and 8am, 8am to 9am, etc - for all 24 hour blocks in day. I do not need them plotted by day of the week or month, just time in the day. Each timestamp is in UTC.
can someone help me?
You could use MySQL's FROM_UNIXTIME function to get date strings from the database, and dump the results into a file, which you can subsequently read into MATLAB. Next, one of the ways to extract the time of day of each record is to use MATLAB's datevec function, to get each component of the date string seperately:
datevec('2007-11-30 10:30:19')
ans =
2007 11 30 10 30 19
For instance, if you read in the data as one long vector with date strings, you could apply datevec to this vector, and subsequently grab the 'hour column' of the resulting matrix. Then, you can make a histogram of the counts using the hist or histc functions, depending on whether you want to specify bin centers or bin edges. If you have an hour column H, something like hist(H, 0:23) should work. The histc function might be a bit more natural for the nature of your data, but is slightly more involved; check the documentation.