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
2017년 12월 28일
When you ran the code, you did not pass in the name of the file that you wanted to read.
댓글 수: 1
Walter Roberson
2019년 2월 7일
sum(ismember(fileread(FileName), '0':'9'))
No fopen/fread needed.
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
2018년 3월 11일
For "any of the characters, 0-to-9" isstrprop(fileChars, 'digit') is more efficient. Or:
sum(fileChars >= '0' & fileChars <= '9')
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
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
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
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
2018년 6월 3일
Thanks for the prompt feedback @Walter. Replacing 'end' statement with an 'else' after the if statement solved the issue.
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!