Averaging each .STD files
2 次查看(过去 30 天)
显示 更早的评论
Greetings. I have been pondering and working hard on this issue since I came here to ask for solutions. I have over 100 .std files in which each of them has 10 headers and each header has 24 hours data of which each hour has 60 values. I want to loop through the hundred data files and get the average of the 24hrs data with graphs. I was only able to do for each data at a time and it's very tedious.
2 个评论
Steve Eddins
2020-10-28
If you can provide more information about the content and structure of your files, it might help people come up with answers for you. It would be ideal if you could attach a couple of sample data files.
Based on your description so far, I would probably be looking to create a timetable containing all the data and then use the retime function to get 24-hour averages.
采纳的回答
Steve Eddins
2020-10-29
编辑:Steve Eddins
2020-10-29
[Update: Based on comments from Moses, I submitted another answer to this question. See below.]
Try this:
files = dir("*.Std");
N = numel(files);
Filename = strings(N,1);
Date = NaT(N,1);
Mean1 = zeros(N,1);
Mean2 = zeros(N,1);
Mean3 = zeros(N,1);
Mean4 = zeros(N,1);
for k = 1:numel(files)
name = string(files(k).name);
A = readmatrix(name,"FileType","text");
[~,base_name,~] = fileparts(name);
base_name_parts = split(base_name,"-");
Filename(k) = name;
Date(k) = datetime(join(base_name_parts(2:4),"-"));
means = mean(A,1);
Mean1(k) = means(1);
Mean2(k) = means(2);
Mean3(k) = means(3);
Mean4(k) = means(4);
end
T = timetable(Date,Filename,Mean1,Mean2,Mean3,Mean4);
When I run this with your data files, T is this:
8×5 timetable
Date Filename Mean1 Mean2 Mean3 Mean4
___________ ________________________ ______ ______ ______ _____
01-Jan-2015 "bjco001-2015-01-01.Std" 11.992 24.82 2.6294 6.38
02-Jan-2015 "bjco002-2015-01-02.Std" 11.992 22.25 2.1003 6.38
03-Jan-2015 "bjco003-2015-01-03.Std" 11.992 25.834 1.9662 6.38
04-Jan-2015 "bjco004-2015-01-04.Std" 11.992 32.088 1.6014 6.38
05-Jan-2015 "bjco005-2015-01-05.Std" 11.992 25.03 1.8935 6.38
06-Jan-2015 "bjco006-2015-01-06.Std" 11.992 31.423 1.5822 6.38
07-Jan-2015 "bjco007-2015-01-07.Std" 11.992 31.528 1.8784 6.38
08-Jan-2015 "bjco008-2015-01-08.Std" 11.992 31.08 1.6912 6.38
...
Plot sample:
plot(T.Date,T.Mean2)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/396368/image.png)
更多回答(3 个)
Steve Eddins
2020-10-29
Try this:
files = dir("*.Std");
N = numel(files);
Date = NaT(0,1);
X = zeros(0,3);
for k = 1:numel(files)
name = string(files(k).name);
A = readmatrix(name,"FileType","text");
[~,base_name,~] = fileparts(name);
base_name_parts = split(base_name,"-");
Date = [Date ; datetime(join(base_name_parts(2:4),"-")) + (A(:,1)/24)];
X = [X ; A(:,2:4)];
end
T = timetable(Date,X(:,1),X(:,2),X(:,3));
T = sortrows(T);
head(T)
ans =
8×3 timetable
Date Var1 Var2 Var3
____________________ _____ ____ ____
01-Jan-2015 00:00:00 12.72 2.69 6.38
01-Jan-2015 00:01:01 12.69 2.47 6.38
01-Jan-2015 00:01:58 12.66 2.54 6.38
01-Jan-2015 00:03:00 12.63 2.86 6.38
01-Jan-2015 00:04:01 12.6 2.39 6.38
01-Jan-2015 00:04:58 12.57 2.52 6.38
01-Jan-2015 00:06:00 12.53 2.81 6.38
01-Jan-2015 00:07:01 12.49 2.65 6.38
T2 = retime(T,'hourly','mean');
head(T2)
ans =
8×3 timetable
Date Var1 Var2 Var3
____________________ ______ _______ ____
01-Jan-2015 00:00:00 10.85 2.0272 6.38
01-Jan-2015 01:00:00 8.4937 1.8062 6.38
01-Jan-2015 02:00:00 7.681 1.1152 6.38
01-Jan-2015 03:00:00 6.0948 1.016 6.38
01-Jan-2015 04:00:00 3.7198 1.0692 6.38
01-Jan-2015 05:00:00 3.066 1.1472 6.38
01-Jan-2015 06:00:00 8.8583 0.97183 6.38
01-Jan-2015 07:00:00 18.533 1.1425 6.38
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!