search number in a text file

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

what should i change here?
strc = textscan(fopen('yourtxt.txt'),'%c');
s1 = regexp(strc{:}', '[0-9]*','match');
yournumber = str2double(s1);
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.

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

Hi Andrei thanks for the answer
but i just need to return true or false if there is numbers in the given file... and i need to do it with a loop...
Thank you very much for your help!!
Why a for loop?
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

Tags

Asked:

on 24 Apr 2011

Community Treasure Hunt

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

Start Hunting!