Processing .mat files via a for loop
조회 수: 8 (최근 30일)
이전 댓글 표시
Hi there,
I would like to read in these .mat files and process them via a for loop
Firstly, I need to replace all values above 700 in column 2 (PAR) with NaN
Secondly, I need to find all missing datetimes from column 1 (DateTime) and fill these in (datetime should be running every 5 min), the corresponding value in column 2 can be NaN
I have attempted to create a for loop and believe that the table needs to be converted into a timetable to be able to use greater than (>)?
I am new to for loops, but it would be good to learn how to do all of this processing within the for loop rather than for each separate file.
TIA
댓글 수: 2
Dyuman Joshi
2023년 6월 7일
"Firstly, I need to replace all values above 700 in column 2 (PAR) with NaN"
Use logical indexing.
"I have attempted to create a for loop and believe that the table needs to be converted into a timetable to be able to use greater than (>)?"
Access the elements of table via indexing -
LastName = ["Sanchez";"Johnson";"Zhang";"Diaz";"Brown"];
Age = [38;43;38;40;49];
Smoker = [true;false;true;false;true];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
patients = table(LastName,Age,Smoker,Height,Weight,BloodPressure)
%Access elements of a column
out=patients.(4)
%Access a particular element via {}
patients{3,4}
patients{3,2}
patients{3,4}>patients{3,2}
Also, please share your code.
답변 (1개)
Swastik Sarkar
2023년 6월 8일
Assuming each MAT-File holds only one variable, we can load it using this
tmpC = struct2cell(load(filename));
myVar = tmpC{1};
For more information go to this [link](https://in.mathworks.com/matlabcentral/answers/723348-get-unknown-variable-from-mat-file#answer_603208)
I have updated the provided script to load `MAT-File` and process them in the same for loop you are loading them from.
myFolder = 'processing/raw_data';
filePattern = fullfile(myFolder,'PAR_*.mat');
matFiles = dir(filePattern);
for k = 1: length(matFiles)
matFilename = fullfile(myFolder,matFiles(k).name);
tmpC = struct2cell(load(matFilename));
PAR = tmpC{1};
PAR = table2timetable(PAR)
idx = any(PAR{:,:} > 700, 2)
end
I hope you can now modify this further to fulfill your requirements of data processing.
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!