Processing .mat files via a for loop

14 views (last 30 days)
Sophia
Sophia on 7 Jun 2023
Commented: Sophia on 8 Jun 2023
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 Comments
Dyuman Joshi
Dyuman Joshi on 7 Jun 2023
"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 on 7 Jun 2023
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

Sign in to comment.

Answers (1)

Swastik Sarkar
Swastik Sarkar on 8 Jun 2023
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 Comment
Sophia
Sophia on 8 Jun 2023
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

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!