Greetings all.
My name is Joseph Moses.
I would like this great house to help me on how to average my data into hour and the hourly averaged data into daily.
For instance,
1.How do i get the corresponding average of 0.00:0.983, 1.000:1.983 till 23.00:23.983 in the second column (VTEC Data) from the below data or the attached.
2. Then plot the result Time against VTEC

1 comentario

Ameer Hamza
Ameer Hamza el 19 de Mzo. de 2020
No need to include the complete txt file in the question. I have deleted to make the question readable.

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 19 de Mzo. de 2020
Editada: Star Strider el 19 de Mzo. de 2020
Try this:
D = load('bjcoo001-2015-01-01.txt');
M = ceil(D(:,1)+1E-4);
HrMeans = accumarray(M, (1:numel(M)).', [], @(x){mean(D(x,2:4))});
Result = [(0:23).' cell2mat(HrMeans)]
figure
plot(Result(:,1), Result(:,2))
grid
xlabel('Time')
ylabel('VTEC')
producing:
Result =
0 10.8497 2.0272 6.3800
1.0000 8.4937 1.8062 6.3800
2.0000 7.6810 1.1152 6.3800
3.0000 6.0948 1.0160 6.3800
4.0000 3.7198 1.0692 6.3800
5.0000 3.0660 1.1472 6.3800
6.0000 8.8583 0.9718 6.3800
7.0000 18.5332 1.1425 6.3800
8.0000 26.3258 1.4597 6.3800
9.0000 32.8312 1.5363 6.3800
10.0000 39.3363 1.9443 6.3800
11.0000 45.3585 3.1638 6.3800
12.0000 49.2812 3.2907 6.3800
13.0000 51.2610 2.2868 6.3800
14.0000 53.3373 2.2015 6.3800
15.0000 49.9703 4.1498 6.3800
16.0000 45.2548 3.5810 6.3800
17.0000 37.1800 3.4638 6.3800
18.0000 24.5763 3.7843 6.3800
19.0000 16.1022 4.9532 6.3800
20.0000 14.7298 3.7925 6.3800
21.0000 14.8348 4.0482 6.3800
22.0000 13.5453 3.9738 6.3800
23.0000 14.4595 5.1812 6.3800
EDIT —
Added plot image —

6 comentarios

Moses Joseph
Moses Joseph el 23 de Mzo. de 2020
Editada: Moses Joseph el 23 de Mzo. de 2020
It works correctly. You woudn't know what graet thing you have done for me. I have been on this for over 3 weeks now.
I am deeply grateful sir. Thank you a whole lot.
But please can you help me with the folowing:
1. Kindly explain the code to me
2. What if I want to do same for 365 files of such data automatically?
3. Then how do I do daily, weekly and mothly, quarterly avearge for the 365 days data files?
If you don't mind I send the files via your mail address sir. mine is mosejroy@gmail.com
Thank you sir
Star Strider
Star Strider el 23 de Mzo. de 2020
As always, my pleasure!
(1.) The ‘M’ variable creates the individual hour designations as a vector of integers. The accumarray call then uses that vector and a vector of the linear indices (the second argument), to create a cell array using the anonymous function in the fourth argument (that takes the means of the matching columns 2 through 4). (The accumarray function is a very efficient set of loops.) It returns those mean values as a cell array. The ‘Result’ variable then presents those values (along with a synthesized hour vector in the first column).
(2.) Put my code in a loop that reads and processes each file, then save ‘Result’ as a cell array (easiest) for each file. See: Import or Export a Sequence of Files for hellp with that. The index of the cell array would correspond to the day.
(3.) I am not certain what you want to do. It would likely be best to simply use a loop, indexing it appropriately to get the result you want.
Moses Joseph
Moses Joseph el 9 de Abr. de 2020
Greetings.
Once again, thank you for the previous help.
I still can't work out the the solution you gave for export.
But maybe from the question I am about to ask will enable solve part of question 3.
What if I have 5 or more txt files ranging from bjcoo001-2015-01-01.txt 2015-01-05.txt, how do I achieve the same result as the above for each of the 5 files?
Thank you for your usual assistance sir
As always, my pleasure.
I would do something like this to read them:
numFiles = 5;
for k = 1:numFiles
filename = sprintf('bjcoo001-2015-01-%02d.txt', k)
D = load(filename);
M = ceil(D(:,1)+1E-4);
HrMeans = accumarray(M, (1:numel(M)).', [], @(x){mean(D(x,2:4))});
Result{k} = [(0:23).' cell2mat(HrMeans)]
end
and similarly for saving the ‘Result’ cell array to new files.
To write them in a similar loop, consider using writecell for this, although other options are possible. Also consider using fullfile to be certain the files are written to the directory where you want to put them (and also from where you want to read them).
I can’t test this because I don’t have the files. However since it uses my existing code and only creates the individual file names (and that appears to work correctly), it should work.
Moses Joseph
Moses Joseph el 9 de Abr. de 2020
Thank you very much sir.
Star Strider
Star Strider el 9 de Abr. de 2020
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Ameer Hamza
Ameer Hamza el 19 de Mzo. de 2020
Editada: Ameer Hamza el 19 de Mzo. de 2020
Try this
M = readmatrix('bjcoo001-2015-01-01.txt');
time = t(:,1);
hours = floor(time);
hour_average = splitapply(@(x) mean(x(:, 2:end), 1), M, hours+1);
hour_average = [unique(hours) hour_average];
plot(hour_average(:,1), hour_average(:,2:4));

2 comentarios

Moses Joseph
Moses Joseph el 23 de Mzo. de 2020
Thank you very much on this but I am getting Undefined function or variable 'readmatrix'. How do I defined it again since it was already defned?
Ameer Hamza
Ameer Hamza el 23 de Mzo. de 2020
I guess you are using a release older than R2019a. readmatrix was added in R2019a.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 19 de Mzo. de 2020

Comentada:

el 9 de Abr. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by