Question

I have a data.txt file like:

123 124 125 126

I want to compare these number against num

My code is like this

 data= textread('data.txt','%d');
    num = 125;
    if num == data
         b = 1;
    else
         b = 0;
    end

but answer shows 0 ( b = 0 )

how to scan these numbers?

No correct solution

OTHER TIPS

Here is a quote from the doc page of eq function (the functional form of == operator):

A == B

If one input is scalar and the other a nonscalar array, then the scalar input is treated as if it were an array having the same dimensions as the nonscalar input array. In other words, if input A is the number 100, and B is a 3-by-5 matrix, then A is treated as if it were a 3-by-5 matrix of elements, each set to 100. MATLAB returns an array of the same dimensions as the nonscalar input array.

And here is the relevant section from the docs of if statement:

if expression
    statements
end

An evaluated expression is true when the result is nonempty and contains all nonzero elements (logical or real numeric). Otherwise, the expression is false.

So perhaps you meant to use:

if any(data == num)
    disp('number found')
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top