search number in a text file

2 views (last 30 days)
Ben Gedi
Ben Gedi on 24 Apr 2011
Hello
i am new in matlab and i want to know how to search numbers in a
text file for example:
the content of the text file is:
abc
afsa
fas54fg
dsa
the program suppose to return false because there is number ("54") in line 3
Thanks

Answers (2)

Walter Roberson
Walter Roberson on 24 Apr 2011
Use textscan(). Set the Whitespace characters to include everything except the digits. If textscan() returns a non-empty input then the file contained at least one number.
  2 Comments
Ben Gedi
Ben Gedi on 24 Apr 2011
what should i change here?
strc = textscan(fopen('yourtxt.txt'),'%c');
s1 = regexp(strc{:}', '[0-9]*','match');
yournumber = str2double(s1);
Walter Roberson
Walter Roberson on 24 Apr 2011
WS=[1:47 58:91 93:255 '\\'];
fid = fopen('yourtxt.txt');
strc = textscan(fid, '%d', 'Whitespace', WS);
fclose(fid);
has_no_numbers = isempty(strc{1});
Warning: do not include 0 in your WS, as MATLAB will ignore the list of characters if you do. And you have to take special care about the \ character as MATLAB tries to examine it as a backspace constant such as \n or \t

Sign in to comment.


Andrei Bobrov
Andrei Bobrov on 24 Apr 2011
variant
Your text in the file 'yourtxt.txt'
strc = textscan(fopen('yourtxt.txt'),'%c');
s1 = regexp(strc{:}', '[0-9]*','match');
yournumber = str2double(s1);
variant 2
strc = textscan(fopen('yourtxt.txt'),'%c');
s1 = regexp(strc{:}', '[0-9]*');
no_numbers = isempty(s1);
  3 Comments
Oleg Komarov
Oleg Komarov on 24 Apr 2011
Why a for loop?
Ben Gedi
Ben Gedi on 24 Apr 2011
i know that is not necessary but this the way i need it to be,
but still if you have other solution i will be happy to see it to
thanks
Ben

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!