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.

1 次查看(过去 30 天)
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.

回答(5 个)

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

Image Analyst
Image Analyst 2017-10-7
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 2018-3-11
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 2018-6-3
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

RAMAKANT SHAKYA
RAMAKANT SHAKYA 2019-2-7
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

类别

Help CenterFile Exchange 中查找有关 Dimensionality Reduction and Feature Extraction 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by