Plot multiple data matlab [duplicate] - matlab

This question already has answers here:
Show two different plots in one plot
(2 answers)
Closed 8 years ago.
Good Morning, I have a problem with matlab plot.
I have generated sample of data that belong to different days; the data are the main posture of the human (labelled with 1,2,3,4).
Now I have 30 vector (one for each day) with the number of sample equals to the seconds of the day (about 86400 sample...). I have one posture for each second.
My aim is to plot the distribution of the sample during one month, in X axis I would have the days of the month (1,2,3.....30) and in the Y axis I would have the hour (sample/3600 I think).
How can I plot all the data in only one graph? I have two main problem:
I have 30 vector with different lenght (because I have generated the sample with random function) so the first step is to allineate the data I think because PLOT function needs vectors with the same lenght...
plot 30 days in the same plot, in order to evaluate the whole distribution of the posture in a month
A small example: day1 = [2222111333444] day2 = [22111333333444] day3 = [2221111133334444]. The input are sequences of postures (one sequence for day); now I need to obtain a plot with a "vertical representation" of these postures (on the x axis the days, on the y axis the hour of the day, for each hour I have about 3600 sample-one sample for second). With the command "hold on" no problem but I don't need to overlap the data but I need to place side by side the vector data
Andrea

It goes something like this, but of course,if you have 30 days and one entry per second you would need to use a matrix and sum the individual rows. Also, you don't need to make the vectors the same size, but then you have to use a different parameter for the x axis (Days) everytime.
day1=[2 2 2 2 1 1 1 1 3 3 3 4 4 4];
day2=[2 2 1 1 1 3 3 3 3 3 4 4 4 4];
day3=[2 2 2 1 1 1 1 3 4 4 4 4 4 4];
Days=1:3;
LayingTime=[sum(day1==1),sum(day2==1),sum(day3==1)];
SittingTime=[sum(day1==2),sum(day2==2),sum(day3==2)];
StandingTime=[sum(day1==3),sum(day2==3),sum(day3==3)];
RockingTime=[sum(day1==4),sum(day2==4),sum(day3==4)];
plot(Days,LayingTime,Days,SittingTime,Days,StandingTime,Days,RockingTime)
xlabel('Day')
ylabel('Hours of Activity')
legend('Hours Laying','Hours Sitting','Hours Standing','Hours Rocking')

Related

How to average monthly data using a specific method in Matlab?

I have the following vector of monthly values (vectorA). I put the date related info next to it to help illustrate the task but I work with just the vector itself
dates month_in_q vectorA
31/01/2020 1 10
29/02/2020 2 15
31/03/2020 3 6
30/04/2020 1 8
31/05/2020 2 4
30/06/2020 3 3
How can I create a new vectorNEW according to this algorithm
In each quarter the first month is the original first month
In each quarter the second month is the average of first and second month
In each quarter the third month is the average of all three months
So that I get the following vectorNEW by manipulating the original vectorA in a loop given this the re-occuring pattern above
dates month_in_q vectorA vectorNEW
31/01/2020 1 10 10
29/02/2020 2 15 AVG(10+15)
31/03/2020 3 6 AVG(10+15+6)
30/04/2020 1 8 8
31/05/2020 2 4 AVG(8+4)
30/06/2020 3 3 AVG(8+4+3)
... ... ... ...
An elegant solution was provided by the user dpb on mathworks website.
vectorNEW=reshape(cumsum(reshape(vectorA,3,[]))./[1:3].',[],1);
Further info below
https://uk.mathworks.com/matlabcentral/answers/823055-how-to-average-monthly-data-using-a-specific-method-in-matlab

Vector with specific number of equally spaced values

I am familiar with the command:
(-5:0.1:5)
which creates a vector of values, equally spaced by 0.1, from -5 to 5.
However, is there a way to produce a vector of values, equally spaced from -5 to 5 such that there are, say, 100 values in the vector.
(-5:0.1:5) gives a vector with 101 values, however, is there a way to get a vector of 100 values without manually calculating the step size?
Yes there is. Use function linspace. See documentation here
linspace(1,10,10)
ans =
1 2 3 4 5 6 7 8 9 10
also the question is a duplicate of this question

How to draw a Histogram in Matlab

I have a set of around 35000 data. These data are the signal strengths received only from a single location for different time interval of time. I want to plot a Histogram using these data. My X-axis will give the information about "Signal Strengths" and my Y-axis will give the information about "Probability". My histogram will consists of different bars which will give information about the signal strength and probabilities.
For example, suppose I have the following data
a= [ 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 5 6 6 6 6 6 6 6 6 6 6 6]
How can I plot the graph using data at X-axis and Probability at Y-axis? Any help will be appreciated. Thanks!
This should work just fine if you don't want to use some predefined functions:
una=unique(a);
normhist=hist(a,size(unique(a),2))/sum(hist(a));
figure, stairs(una,normhist)
Una has only the unique values of a, normhist is now between 0 and 1 and it's the probability of occurring of the individual signal because you divide it by the number of elements included in the data.

Set x-axis to non-monotonic values and avoid scaling

I use matlab to plot a graph where instead of having x-axis increase monotonically, I have my own values. eg 5 14 8 9 12 7 etc.I use set (gca,'XTickLabel',num2str(mydata)) which generally works. However, when mydata is more than four or five digits, Matlab scales the graph and thus x-axis values no longer correspond to their intended points. Any ideas on how to prevent this scaling? To clarify, when I make the figure larger, it shows the plot as it should.
The problem is in your num2str() conversion:
mydata = 1:10;
num2str(mydata)
ans =
1 2 3 4 5 6 7 8 9 10
This means, that each tick will be labelled with this long 1 by n char array. The axes will then be resized to fit the labels inside the figure.
A solution is to create one label per row of a char array:
reshape(sprintf('%2d',mydata),2,[])'
ans =
1
2
3
4
5
6
7
8
9
10
Sort of solution is to write set(gca,'xtick',1:myDataVectorLength) before set (gca,'XTickLabel',num2str(mydata))

Matlab - find a missing time from a matrix and insert the missing time with a value

I have a series of times and returns in various matrices lets call them a b c. They are all x by 2 with column 1 being times in seconds and column 2 returns. While all the returns are over a set series of time intervals like 15s 30s 45s etc the problem is not all the matrices have all the time buckets so while a might be a 30 by 2 , b might only be a 28 by 2. Because it is missing say time 45 seconds and a return. I want to go through each matrix and where I am missing a time bucket I want to insert the bucket with a zero return - I am happy to create a control 30 by 1 matrix with all the times that need to be cross referenced
You can use ismember to locate these missing positions, so if a is the control vector and b is the missing data vector ind=find(ismember(a,b)==0); will give you the indices of a that are missing in b.
For example:
a=1:10;
b=[1:2 4:5 7:10];
ind=find(ismember(a,b)==0);
ind =
3 6
In order to to add zeros in the right places for b just
for n=1:numel(ind)
b=[b(1:ind(n)-1) , 0 , b(ind(n):end)];
end
b =
1 2 0 4 5 0 7 8 9 10