I am new to Matlab and I want to make a function called digit_counter that takes the name of a text file as input and returns the number of digits (i.e., any of the characters, 0-to-9) that the file contains. Any help?

 채택된 답변

Stephen23
Stephen23 2016년 5월 20일
편집: Stephen23 2016년 5월 21일

0 개 추천

Assuming only text files, then here is one line to solve your task:
nnz(isstrprop(fileread(...),'digit'))
just replace ... with the filename. If you really want it as a function:
digit_counter = @(name)nnz(isstrprop(fileread(name),'digit'));
and call it like this:
digit_counter('test.txt');

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 20일
편집: Azzi Abdelmalek 2016년 5월 20일

0 개 추천

name='w1o4rd12'
out=numel(regexp(name,'\d+'))
%or
name='w1o4rd12'
a=name-'0'
idx=a>=0 & a<=9
out=nnz(idx)

카테고리

도움말 센터File Exchange에서 Environment and Settings에 대해 자세히 알아보기

질문:

2016년 5월 20일

편집:

2016년 5월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by