Searching for Numbers above a certain Threshold

3 views (last 30 days)
Kevin
Kevin on 20 Nov 2013
Edited: dpb on 21 Nov 2013
Threshold = fopen('dataFile.txt');
ThresholdValue = 0;
while ~feof(Threshold)
lineThreshold = fgetl(Threshold);
class(lineThreshold);
strThreshold = num2str(lineThreshold);
class(strThreshold);
length(strThreshold);
FindNum = regexp(strThreshold, '\d+(.)\d*', 'match');
FindThreshold = str2double(FindNum)
class(FindThreshold);
if FindThreshold > 30.7
ThresholdValue2 = ThresholdValue + 1
end
end
fprintf('Number of numbers greater that the threshold: %d \n', ThresholdValue2)
The code above I am using to search through a file that is made up of lines formatted as such "45.3-12.6-89.3-23.7-12.4 valid" I am attempting to extract each of the numbers and test them to see if they are a value above the threshold of 30.7. When I run the code it displays the variable FindThreshold like this:
FindThreshold =
Columns 1 through 5
25.0000 32.9000 49.1000 64.6000 82.6000
Column 6
98.2000
however when it reaches the fprintf command it only displays the value of 0

Answers (1)

dpb
dpb on 20 Nov 2013
Edited: dpb on 20 Nov 2013
FindThreshold = str2double(FindNum)
if FindThreshold > 30.7
If FindThreshold is a vector as will often be the case, the if is T iff all elements in the vector are T. Since at least one isn't, the if...end clause is never executed.
Depending on what you actually want, either
if any(FindThreshold) > 30.7
ThresholdValue2 = ThresholdValue + 1
will increment the counter on a per line basis or
ThresholdValue2 = ThresholdValue2 + sum(FindThreshold > 30.7);
will count the total number of entries above the threshold value.
As an aside it would be faster to read the file and operate on the overall array. For example from the single line,
>> s='45.3-12.6-89.3-23.7-12.4 valid';
>> d=cell2mat(textscan(s,repmat('%f',1,6),'treatasempty','valid'))
d =
45.3000 -12.6000 -89.3000 -23.7000 -12.4000 NaN
>> d>30.7
ans =
1 0 0 0 0 0
>> any(d>30.7)
ans =
1
>> sum(d>30.7)
ans =
1
>> all(d>30.7)
ans =
0
>> sum(d<30.7)
ans =
4
>>
ADDENDUM:
Also as an aside, the class statements serve no useful purpose here...
  3 Comments
Kevin
Kevin on 21 Nov 2013
Also I believe that the values of (FindThreshold) are in a CELL array
dpb
dpb on 21 Nov 2013
Edited: dpb on 21 Nov 2013
Again, because the if isn't getting executed because Matlab defines '==' as a vector comparison elementwise in computing and if() as being T iff
all(logical_expression) = 1
On the input format, I wondered about that initially, assumed that the '-' was a delimiter first then came back and changed it... :(
In that case
>> d=cell2mat(textscan(s,repmat('%f',1,6), ...
'treatasempty','valid', 'delimiter','-'))
d =
45.3000 12.6000 89.3000 23.7000 12.4000 NaN
>>
NB: the cell2mat around the textscan call to convert to a regular array.
ADDENDUM: Or, on the input format -- alternatively you can just use the '%f' to read and then abs() the result, too; same result in the end.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!