Reputation: 25
I'm trying to read a text file and then count the number of digits ( number 0 to 9) contained in the file . i used fid = fopen('filename','r') to open the file , then i used textscan(fid,'%f') to try to get the digits but it returns an empty (0 by 1) matrix. I also used fscanf but doesn't work. I thought it was the formatspec that was wrong but playing with other format specs doesn't work. please advice
Upvotes: 0
Views: 685
Reputation: 65430
First of all, the format spec %f
attempts to read all numbers in the file in as floating point numbers which isn't quite what you want, it doesn't seem.
If all that you want is the number of digits in the file, just load the whole thing in as a string and search for the digits 0-9.
fid = fopen('filename', 'r');
characters = fread(fid, '*char');
fclose(fid);
% Determine whether each character in the input was a digit between 0 and 9
isDigit = ismember(characters, '0':'9');
% Count the total number of characters that were digits
nDigits = sum(isDigit);
Upvotes: 0