how to find the total no of letters in a text file in matlab

Hi ;
I am going to make a function called letter_counter that takes the name of a text file as input and returns the number of letters (i.e., any of the characters, a-to-z and A-to-Z) that the file contains. i have given that hint: You can use the built-in function isletter. If there is a problem opening the file, the function returns -1.
I am going that code but do not know what i am doing
function m=letter_counter(file)
fid=fopen(file,'rt')
m = char(fread(fid,inf));
fclose
if fid~=fopen(file,'rt')
m=-1;
end
end
I am getting that error
Feedback: Your program made an error for argument(s) 'letter_counter.m'
Your solution is _not_ correct.
I do not know what i have made. Please assist about the correct one. Thanks in advance for assistance...

6 件のコメント

Adam
Adam 2015 年 6 月 8 日
You need to report actual Matlab errors to get efficient help. Reporting errors that I assume come from some 3rd party coursework submission system are not very helpful. Don't you run your code yourself before submitting it?
If you are trying to count the letters in a file then you need to do more than just read them all into a variable and return that.
Stephen23
Stephen23 2015 年 6 月 8 日
@Adam: if you read all of the OP's other questions you will find out the answer. The comments to this question in particular contain an interesting discussion on the topic:
Marcos Mariano
Marcos Mariano 2015 年 6 月 13 日
編集済み: Marcos Mariano 2015 年 6 月 13 日
Try this:
function letterCounter = letter_counter(file)
fid = fopen(file,'rt'); % Permission to read the file
if fid<0
letterCounter = -1; % It is error message, if we have a problem opening the file
return;
end
A = fread(fid,inf,'*char');
letterCounter = sum(isletter(A))% Sum of all the letters in the file
fclose(fid)
end
Tezen Ralkan
Tezen Ralkan 2015 年 7 月 24 日
@Marcos Mariano: Why is there a '*' before 'char'? your function seems to work but I don't know why that charachter is there.
Kapil Dhanwani
Kapil Dhanwani 2016 年 9 月 23 日
what to do if i want to count number of digits(0-9) not letter?
Walter Roberson
Walter Roberson 2016 年 9 月 23 日
You would read the help documentation for isletter() and check out the various routines mentioned in 'See Also'

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

 採用された回答

Image Analyst
Image Analyst 2015 年 6 月 8 日

0 投票

Your m is just all the bytes read. It's not taking into account that there are other characters like punctuation marks that they do not want counted. I don't see where you're checking if the character is in the range a-z or A-Z. Where are you doing anything like
letters = (m >= 'a' & m <= 'z') | (m >= 'A' & m <= 'Z');
That's why it's not giving the correct answer. Now do you know what letters above is? Do you know what you would do with that to count the valid letters?

6 件のコメント

Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 9 日
@Image thanks for contributions.... the letters variables is assigned with logical or operator. Which is checking the value of m in the lowercase and uppercase alphabets. The letters will check this opreations but then how we count the letters?
Walter Roberson
Walter Roberson 2015 年 6 月 9 日
sum() the array of logical results to get the count.
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 10 日
@walter using that but getting an error
function m=letter_counter(file)
fid=fopen(file,'rt')
letters = (m >= 'a' & m <= 'z') || (m >= 'A' & m <= 'Z');
m=sum(letters);
stat = fclose(fid)
end
???
Image Analyst
Image Analyst 2015 年 6 月 10 日
Of course. You're missing the line you had before where you read in the characters into an array called m. And you should not call the sum "m" because that's supposed to be used for your input string. Call it "letterCount". Make sure you return letterCount, not m.
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 11 日
@image i do not gets your view... where i required a line for the collection of strings? how can i store these strings????
Image Analyst
Image Analyst 2015 年 6 月 13 日
There is no line like this:
m = char(fread(fid,inf));
like you had before. Why did you remove that line?

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

カテゴリ

ヘルプ センター および File ExchangeScripts についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by