Averaging each .STD files
이전 댓글 표시
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.
Moses Joseph
2020년 10월 28일
채택된 답변
추가 답변 (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
댓글 수: 14
Moses Joseph
2020년 10월 29일
Moses Joseph
2020년 10월 29일
Steve Eddins
2020년 10월 29일
Did you copy-and-paste the code here directly into a code file in your MATLAB editor, or did you retype it manually? If you retyped it, then I suspect a typing mistake.
Include the complete error message here, including the line of code where the error appeared.
For troubleshooting a program, I highly suggest using the MATLAB Debugger. To get started, set a breakpoint on the first line of the file, run the code, and then single-step through each code line. Look at the values that get assigned at each line and make sure they are what you expect.
Moses Joseph
2020년 10월 29일
Moses Joseph
2020년 10월 29일
Steve Eddins
2020년 10월 29일
To troubleshoot this, you need to examine the sizes and values of each variable at each step to determine where something is going wrong. Here, the variables base_name_parts and A being indexed. Do they have the expected size here? If not, why not? For example, if some of your filenames have a different form than the set you attached, then maybe base_name_parts isn't always a 4-element string vector.
Or maybe you'll find that A is empty just before the error occurs, which would suggest that the call to readmatrix failed for some reason, which in turn might suggest that one or more files in your dataset is malformed.
Moses Joseph
2020년 10월 29일
Moses Joseph
2020년 10월 29일
Moses Joseph
2020년 10월 29일
Moses Joseph
2020년 10월 29일
Moses Joseph
2020년 10월 29일
Steve Eddins
2020년 10월 30일
I'm glad you were able to figure it out!
Moses Joseph
2020년 10월 30일
Moses Joseph
2020년 10월 31일
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
