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
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
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
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
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
2016년 9월 23일
what to do if i want to count number of digits(0-9) not letter?
Walter Roberson
2016년 9월 23일
You would read the help documentation for isletter() and check out the various routines mentioned in 'See Also'
채택된 답변
추가 답변 (1개)
charu sharma
2015년 8월 27일
0 개 추천
카테고리
도움말 센터 및 File Exchange에서 Scripts에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!