Filtering imported data from a .mat file

15 views (last 30 days)
anna diederichs
anna diederichs on 24 Jul 2018
Answered: Avni Agrawal on 10 Sep 2024
hello. I have a .mat file that I am calling on in my code. The .mat file is a 1x104 struct with 15 fields. within this, I want to filter the imported data by column number 9. If the value in this column = 1, I want Matlab to use the data in that row. if the value in that column =0, I do not want Matlab to use it when it is imported. please help me on how to write an if statement for this problem. Thank you in advance.

Answers (1)

Avni Agrawal
Avni Agrawal on 10 Sep 2024
I understand that you are trying to filter the data in a `.mat` file based on a specific field within a structure, you can load the file into MATLAB, access the desired field, and then use logical indexing to filter the data.
Here's how you can achieve this:
  1. Load the .mat File: Use the `load` function to bring your data into the workspace.
  2. Access the Structure: Extract the structure from the loaded data.
  3. Filter the Data: Use logical indexing to filter the structure based on the condition you specified.
Here's a sample script that demonstrates these steps:
data = load('exampleData.mat'); % Ensure 'exampleData.mat' is the correct file name
% Access the structure
myData = data.myData; % Ensure 'myData' is the correct variable name
% Extract the 'status' field
statusField = [myData.status];
% Filter the structure where 'status' equals 1
filteredData = myData(statusField == 1);
% Display the filtered structure
disp(filteredData);
I hope this helps!

Community Treasure Hunt

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

Start Hunting!