Processing .mat files via a for loop

7 次查看(过去 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
Swastik 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 中查找有关 Data Type Identification 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by