How to count the number of each character from a text file (and the frequency of it)?
Mostra commenti meno recenti
Create a function that takes a text file as input and returns a vector of size 26 with the frequency in percent of each character a, b, . . . z (not sensitive to case.) The frequency must be computed as the number of occurences divided by the total number of characters from a to z that occur, multiplied by one hundred. All other letters such as ø and ä as well as all symbols must be ignored.
Risposta accettata
Più risposte (3)
Mounic Kumar
il 25 Giu 2019
function charnum = char_counter(fname,A)
fid = fopen(fname,'rt');
if fid < 0
charnum = -1;
return
end
if fid >0 && ischar(A)
k=0;
oneread = fgets(fid);
while ischar(oneread)
k = k + count(oneread,A);
oneread = fgets(fid);
end
charnum = k;
else
charnum = -1;
end
fclose(fid);
Amit Dubey
il 15 Mag 2020
Modificato: Amit Dubey
il 2 Giu 2020
function n=char_counter(fname,character)
fid=fopen(fname,'rt');
if fid<0
n=-1;
return;
elseif ~ischar(character)
n=-1;
return;
end
end
c=0;
ol=fgets(fid);
while ischar(ol)
c=c+count(ol,character);
ol=fgets(fid);
end
n=c;
fclose(fid);
2 Commenti
Walter Roberson
il 15 Mag 2020
If the file open works but ischar(character) fails, then you leave the file open when you return.
Amit Dubey
il 2 Giu 2020
Thank you, just made required modifications
Priyamvada Shankar
il 23 Mar 2019
Modificato: Priyamvada Shankar
il 23 Mar 2019
0 voti
Please Help me with this ..function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This file should have exactly three a-s..."
8 Commenti
Priyamvada Shankar
il 23 Mar 2019
Modificato: Priyamvada Shankar
il 23 Mar 2019
What is it? If you know the answer please help me.
Walter Roberson
il 23 Mar 2019
You did not ask a question, so there is no answer.
Priyamvada Shankar
il 23 Mar 2019
Okay thank you so much
Walter Roberson
il 23 Mar 2019
The link I posted describes how to ask the kinds of questions that people here are willing to assist with.
%can any one pls help me what is the cause of error in this.
%ERROR -->Test with all visible characters
%Variable charnum has an incorrect value.
%When testing with ' ' your solution returned 1 which is incorrect. (75444)
function charnum = char_counter(fname,character)
fid = fopen(fname,'rt');
if fid < 0 || ~ischar(character) || ~ischar(fname) || length(fname)==0 || length(character)==0 || isempty(fname) || isempty(character)
charnum=-1;
else
l=[];
oneline = fgets(fid);
while ischar(oneline)
l=oneline;;
oneline = fgets(fid);
end
z=count(l,character);
if z>0
charnum=z;
elseif z<0
charnum=-1;
else
charnum=-1;
end
end
end
Walter Roberson
il 26 Mar 2019
Why are you skipping down to the last line of the file?
Walter Roberson
il 26 Mar 2019
note: your function returns -1 if the character is valid but not found. Your function should only be returning -1 if the file is not found or if the input is not a valid scalar character.
Categorie
Scopri di più su Text Data Preparation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!