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 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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.
Risposte (5)
Walter Roberson
il 28 Dic 2017
When you ran the code, you did not pass in the name of the file that you wanted to read.
1 Commento
Walter Roberson
il 7 Feb 2019
sum(ismember(fileread(FileName), '0':'9'))
No fopen/fread needed.
Image Analyst
il 7 Ott 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
2 Commenti
Jan
il 11 Mar 2018
For "any of the characters, 0-to-9" isstrprop(fileChars, 'digit') is more efficient. Or:
sum(fileChars >= '0' & fileChars <= '9')
Srishti Saha
il 11 Mar 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
1 Commento
Jan
il 11 Mar 2018
The question was asked on 7 Oct 2017 and I think it is no problem, that you have solved this homework now.
Ranil Fernando
il 3 Giu 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 Commenti
Walter Roberson
il 3 Giu 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
il 3 Giu 2018
Thanks for the prompt feedback @Walter. Replacing 'end' statement with an 'else' after the if statement solved the issue.
RAMAKANT SHAKYA
il 7 Feb 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
0 Commenti
Vedere anche
Categorie
Scopri di più su Data Distribution Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!