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?

 Respuesta aceptada

Stephen23
Stephen23 el 20 de Mayo de 2016
Editada: Stephen23 el 21 de Mayo de 2016

0 votos

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');

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 20 de Mayo de 2016
Editada: Azzi Abdelmalek el 20 de Mayo de 2016

0 votos

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

Categorías

Más información sobre Environment and Settings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 20 de Mayo de 2016

Editada:

el 21 de Mayo de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by