MATLAB Answers

Matlab Text file I/o

조회 수: 37(최근 30일)
Priyamvada Shankar
Priyamvada Shankar 24 Mar 2019
댓글: Walter Roberson 23 Mar 2020 0:08
Write a 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..." charnum = char_counter('simple.txt','a') charnum = 3

  댓글 수: 4

표시 이전 댓글 수: 1
Walter Roberson
Walter Roberson 24 Mar 2019
Because you specify that it is a text file, the required output is poorly defined for the cases where the character to search for is either carriage return or newline.
Paul Braverman
Paul Braverman 1 Feb 2020
They key is to use the fileread function
function charnum = char_counter(fname,character)
if ischar(character)==0 | fopen(fname)==-1
charnum=-1
else
A=double(char(fileread(fname)))';
charnum=sum(A==double(character));
end
end
Walter Roberson
Walter Roberson 1 Feb 2020
You leak open files. You open the file but do not close it. MATLAB does not automatically close files when you return from a function.

로그인 to comment.

답변(2개)

Kodavati Mahendra
Kodavati Mahendra 24 Mar 2019
charnum = char_counter('simple.txt','a')
function charnum = char_counter(a,b);
f = fopen(a);
c = textscan(f,'%s');
charnum = sum(sum(char(c{:})==b));
end
Something lilke this?

  댓글 수: 13

표시 이전 댓글 수: 10
Priyamvada Shankar
Priyamvada Shankar 24 Mar 2019
Thank you so much
paul mary
paul mary 24 Mar 2019
@Walter Roberson, Thank you so much, you gave me the best tip.
So Priyamvada, I can give you some hints.
  1. does the file exist? think about function exist() in matlab documentation
  2. think about count() function
  3. Using fread() function is the right thing to do.
asad jaffar
asad jaffar 20 Apr 2019
@priyamvada shanker ,can you kindly guide me ? i am using the same code of yours but it is giving error ,can someone here give me few hints or tell me about the alogrithm.

로그인 to comment.


sadek kouz
sadek kouz 18 Mar 2020 22:12
i tried this but i still got an error for
Assessment result: incorrectTest with all visible characters
Variable charnum has an incorrect value. When testing with ' ' your solution returned 2 which is incorrect. (75444)
function charnum = char_counter(filename,character)
charnum=0;
if ~ischar(character)
charnum=-1;
return;
end
if length(character)~=1
charnum=-1;
return;
end
fid = fopen(filename,'rt');
if fid<0
charnum=-1;
return;
end
oneline= fgets(fid);
while ischar(oneline)
a=sprintf('%s \n',oneline);
c=strfind(a,character);
charnum=length(c);
oneline=fgets(fid);
end
end

  댓글 수: 1

Walter Roberson
Walter Roberson 23 Mar 2020 0:08
charnum=length(c);
That only counts for that one line. You need to create a total for the entire file.
I am not clear as to why you are bothering to do the sprintf and searching that?
Notice that your sprintf() is adding a newline and a space to every line it processes. That is going to lead to incorrect counts whenever you are asked to count newlines or spaces.

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

제품


Translated by