- Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
- Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
- Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
AVERAGE VALUES FOR .XLSX AND PLOT MATLAB R2021a
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Willian
el 15 de Oct. de 2024
Comentada: Walter Roberson
el 16 de Oct. de 2024
I want to calculate the hour by hour average values of the Var3 column of the attached file, I have tried the following code but I could not solve the problem, then I want to graph the average data vs time
I am trying to run this code for .cvs files but applied it for .xlsx files but the code does not run
data1 = readtable('historico_12.xlsx', 'VariableNamingRule','preserve');
MyDateTime = data1.Date + data1.Time;
MyDateTime.Format = 'yyyy-MM-dd HH:mm:ss';
data2 = [data1(:,1) table(MyDateTime) data1(:,[3:end])];
%%
% Create a new column for the time (round up to the start of the hour)
data2.Hour = dateshift(data2.MyDateTime, 'start', 'hour');
% Group by new time column and calculate average
averageData = groupsummary(data2, 'Hour', 'mean', 'Var3');
% Show results
disp(averageData);
% Graph average hourly data
figure;
plot(averageData.Hour, averageData.mean_Var3, '-');
xlabel('Date & Time');
ylabel('Average value Pressure');
title('Average Hour by Hour');
grid on;
% Save results to a new CSV file
writetable(averageData, 'average_hours_by_hour.csv')
I'm trying to adapt it to the following code:
clear;
clc;
%% obtener información sobre los archivos
S = dir('*.xlsx');
%% construir nombres de archivos de ruta completa
filenames = fullfile({S.folder},{S.name});
%% lee cada archivo en una tabla, almacenado en la matriz de estructura S
%como campo 'datos'
for ii = 1:numel(S) %cuenta cuantos archivos hay que almacenar
S(ii).data = readtable(filenames{ii});
end
%% % combine all tables into one
T = vertcat(S.data);
%% eliminar filas con tiempos duplicados
[t,idx] = unique(T.(1)+days(T.(2)));
T = T(idx,:);
%% plot
figure
plot(t,T.(3),'-','LineWidth',1)
grid on
axis tight
%% Simbología
%figure('Name','Measured Data');
xlabel('time (hours)')
ylabel('pressure')
title('WELL')
grid on
grid minor
hold on
% Save results to a new XLSX file
writetable(averageData, 'average_hours_by_hour.xlsx')
4 comentarios
Walter Roberson
el 16 de Oct. de 2024
[t,idx] = unique(T.(1)+days(T.(2)));
That is creating t as a datetime array
t.Hour = dateshift(t, 'start', 'hour');
That is attempting to create a field named Hour within the datetime array t
You probably wanted
T.Hour = dateshift(t, 'start', 'hour');
and
averageData = groupsummary(T, 'Hour', 'mean', 'Var3');
Respuesta aceptada
dpb
el 16 de Oct. de 2024
tH=readtable('historico_12.xlsx');
tH.Properties.VariableNames(1)={'Date'};
tH.Date.Format='yyyy-MM-dd HH:mm:ss';
height(tH)
[head(tH,5);tail(tH,5)]
[min(tH.Var2) max(tH.Var2) mean(diff(tH.Var2))*1E6]
plot(tH.Var2)
nDays=day(tH.Date(end))-day(tH.Date(1))==numel(find(diff(tH.Var2)<0))
So, it appears @Walter Roberson's hypothesis is correct; we'll add the time to the day. As he notes, it's unusual but appears to be what was done when writing the timestamp.
It would seem to do the hourly averages, the easiest route would be to just convert to the timetable and retime...
tH.Properties.VariableNames(2:3)={'DayFraction','Unknown'};
tH.Date=tH.Date+tH.DayFraction;
tH=removevars(tH,'DayFraction');
tH=table2timetable(tH);
head(tH,5)
tHrAvg=retime(tH,'hourly',@mean);
[head(tHrAvg);tail(tHrAvg)]
Más respuestas (0)
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
