search number in a text file
2 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
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
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
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
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!