Processing .mat files via a for loop

조회 수: 8 (최근 30일)
Sophia
Sophia 2023년 6월 7일
댓글: Sophia 2023년 6월 8일
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
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)
patients = 5×6 table
LastName Age Smoker Height Weight BloodPressure _________ ___ ______ ______ ______ _____________ "Sanchez" 38 true 71 176 124 93 "Johnson" 43 false 69 163 109 77 "Zhang" 38 true 64 131 125 83 "Diaz" 40 false 67 133 117 75 "Brown" 49 true 64 119 122 80
%Access elements of a column
out=patients.(4)
out = 5×1
71 69 64 67 64
%Access a particular element via {}
patients{3,4}
ans = 64
patients{3,2}
ans = 38
patients{3,4}>patients{3,2}
ans = logical
1
Also, please share your code.
Sophia
Sophia 2023년 6월 7일
Hi Dyuman,
Thanks for your answer but as I am using files with a lot of data is there not an easier way to access elements of the table without indexing?
The code is attached to my question as a .m file

댓글을 달려면 로그인하십시오.

답변 (1개)

Swastik Sarkar
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.
  댓글 수: 1
Sophia
Sophia 2023년 6월 8일
Hi Swastik,
Thanks for your answer but I don't think this solves any of this issues in my question
I still need a for loop that reads in the .mat files and replace all values above 700 in column 2 (PAR) with NaN

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by