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

조회 수: 3 (최근 30일)
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

채택된 답변

Karim
Karim 2022년 9월 19일
편집: Karim 2022년 9월 19일
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
Voss
Voss 2022년 9월 19일
편집: Voss 2022년 9월 19일
@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

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

추가 답변 (1개)

Chunru
Chunru 2022년 9월 19일
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

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

카테고리

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

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by