Borrar filtros
Borrar filtros

Write 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. If there is a problem opening the file, the function returns -1.

2 visualizaciones (últimos 30 días)
My code:
function[c]=digit_counter(filename)
fid=fopen(filename,'rt');
if fid<0
error('error opening file %s', filename);
end
A = char(fread(fid,inf)).';
c=length(A);
fclose(fid);
end
I am getting the error:
Not enough input arguments.
Error in digit_counter (line 2)
fid=fopen(filename,'rt');
Can somebody pls help me out here.

Respuestas (5)

Walter Roberson
Walter Roberson el 28 de Dic. de 2017
When you ran the code, you did not pass in the name of the file that you wanted to read.

Image Analyst
Image Analyst el 7 de Oct. de 2017
You're not even building the histogram. See if you can finish this:
counts = zeros(1, 10); % Histogram.
fileChars = fileread('test1.m');
for k = 1 : length(fileChars)
theInteger = str2double(fileChars(k));
if ~isreal(theInteger)
% isreal is used because a letter i evaluates as 1 + 1*i (complex)
continue;
end
if ~isnan(theInteger)
% Increment histogram by 1
index = theInteger + 1; % Convert from 0-based to 1-based indexing.
counts(index) = counts(index) + 1;
end
end
counts

Srishti Saha
Srishti Saha el 11 de Mzo. de 2018
This should definitely work:
%function: digit_counter in a file; takes file name as argument
function dc = digit_counter(filename)
dc=-1;
fid= fopen(filename,'r');
if fid>=0
dc= nnz(isstrprop(fileread(filename),'digit'));
fclose(fid);
end
end

Ranil Fernando
Ranil Fernando el 3 de Jun. de 2018
I'm getting a strange error to this problem.
Problem 1 (digit_counter):
Feedback: Your function performed correctly for argument(s) 'digit_counter.m'
Feedback: Your function performed correctly for argument(s) 'day_counter.m'
Feedback: Your function performed correctly for argument(s) 'smallest_multiple.m'
Feedback: Your function performed correctly for argument(s) 'maxproduct.m'
Feedback: Your function performed correctly for argument(s) 'number2letters.m'
Feedback: Your function performed correctly for argument(s) 'circular_primes.m'
Feedback: Your function performed correctly for argument(s) 'cyclotron.m'
Feedback: Your function performed correctly for argument(s) 'huge_add.m'
Feedback: Your program made an error for argument(s) 'Non-existent file. This should generate an error'
Your solution is _not_ correct.
and my code is;
function digi_num = digit_counter(filename)
digi_num = 0;
str_len = [];
digi_ch = {'0','1','2','3','4','5','6','7','8','9'};
fid = fopen(filename,'rt');
if fid < 0
digi_num = -1;
end
str_line = fgets(fid); %read file as string
while ischar(str_line)
str_len = length(str_line);
for ii = 1:str_len
for jj = 1:10
if str_line(ii)==digi_ch{jj}
digi_num = digi_num + 1;
end
end
end
str_line = fgets(fid);
end
  2 comentarios
Walter Roberson
Walter Roberson el 3 de Jun. de 2018
In the case where the open fails, you are still trying to proceed and fgets(fid) even though fid is not valid.
Ranil Fernando
Ranil Fernando el 3 de Jun. de 2018
Thanks for the prompt feedback @Walter. Replacing 'end' statement with an 'else' after the if statement solved the issue.

Iniciar sesión para comentar.


RAMAKANT SHAKYA
RAMAKANT SHAKYA el 7 de Feb. de 2019
function d=digit_counter(filename)
fid=fopen(filename,'r');
if fid<0
d=-1;
else
A = char(fread(fid,inf))';% reading all data from the text file
k=isstrprop(A,'digit'); %determines if elements of input text are of the specified category, numbers
d= nnz(k);% Number of nonzero matrix elements.
fclose(fid);
end
end

Categorías

Más información sobre Dimensionality Reduction and Feature Extraction en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by