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
  2 件のコメント
Jan
Jan 2018 年 3 月 11 日
For "any of the characters, 0-to-9" isstrprop(fileChars, 'digit') is more efficient. Or:
sum(fileChars >= '0' & fileChars <= '9')
Walter Roberson
Walter Roberson 2018 年 3 月 11 日
or sum(ismember(fileChars, '0':'9'))

サインインしてコメントする。


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
  1 件のコメント
Jan
Jan 2018 年 3 月 11 日
The question was asked on 7 Oct 2017 and I think it is no problem, that you have solved this homework now.

サインインしてコメントする。


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
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 6 月 3 日
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 2018 年 6 月 3 日
Thanks for the prompt feedback @Walter. Replacing 'end' statement with an 'else' after the if statement solved the issue.

サインインしてコメントする。


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 Center および File ExchangeDimensionality 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