How to use IF statements to find data greater than or equal to a certain amount

18 views (last 30 days)
If you have a large amount of data, tens of millions of rows of data, how can you use IF statements in MATLAB to find specific data points that spike above a certain threshold?
Lets say for example you have a load of capacitance data against time. And you wan to find where the data spikes above 1 Farad. An example data table:
Time, Capacitance
1 uS, 0.3
2 uS, 0.5
3 uS, 0.8
4 uS, 1
5 uS, 1.3
6 uS, 1.4
7 uS, 1.2
8 uS, 0.9
9 uS, 0.5
You want to sift through this table and create a new variable that only includes data where the capacitance exceeds 1 Farad

Accepted Answer

Karim
Karim on 19 Sep 2022
Edited: Karim on 19 Sep 2022
see below for a demonstration
Time = [1 2 3 4 5 6 7 8 9]';
Capacitance = [0.3 0.5 0.8 1 1.3 1.4 1.2 0.9 0.5]';
MyTable = table(Time,Capacitance)
MyTable = 9×2 table
Time Capacitance ____ ___________ 1 0.3 2 0.5 3 0.8 4 1 5 1.3 6 1.4 7 1.2 8 0.9 9 0.5
% delete variables, just to show that we work with the data from the table
clearvars Time Capacitance
% set a treshold value
Treshold = 1;
% use some logice to find the values larger then the treshold (if you want
% to include it use >= instead of >
KeepMe = MyTable.Capacitance > Treshold;
% extract a part of the table using the logical array
NewTable = MyTable( KeepMe , : )
NewTable = 3×2 table
Time Capacitance ____ ___________ 5 1.3 6 1.4 7 1.2
  6 Comments
Voss
Voss on 19 Sep 2022
Edited: Voss on 19 Sep 2022
@AluminiumMan: You left off the second subscript, like the error message suggests:
A=B.C(C.Capacitance>Threshold,:);
% ^^ you need this part
Notice that @Karim's answer has it:
NewTable = MyTable( KeepMe , : )
Here's a demo with your mat file and variables, as given:
S = load('CapacitanceData.mat');
B = struct('C',S.CapacitanceData.RecordedCapacitanceData);
C = B.C;
Threshold = 1;
A=B.C(C.Capacitance>Threshold,:)
A = 3×2 table
Time Capacitance ____ ___________ 3 1.3 4 1.6 7 1.3

Sign in to comment.

More Answers (1)

Chunru
Chunru on 19 Sep 2022
c = randn(20,1) % random data
c = 20×1
-0.1315 1.3920 0.3866 0.1342 0.7914 -0.5643 2.9497 -0.1842 -0.3139 0.3103
c1 = c(c>1)
c1 = 2×1
1.3920 2.9497
  5 Comments

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!