Finding all visible char in txtfile in MATLAB

1 回表示 (過去 30 日間)
Morgane Aubineau
Morgane Aubineau 2019 年 4 月 11 日
コメント済み: Walter Roberson 2019 年 4 月 18 日
Hi everyone,
I have to write a function that counts the number of a certain character in a text file. 2 input arguments are requested: fname (char vector of the filename) and character (the char it counts in the file).
Output argument: the number of characters found. If the file is not found or character is not a valid char, the function return -1.
I wrote a function which passed correctly three of the 4 tests. The wrong one is
==> 1) Test with all visible characters
Explanation: Variable charnum has an incorrect value. When testing with '#' your solution returned -1 which is incorrect. (0)
This is my code:
function charnum = char_counter(fname, character)
if isequal(isfile(fname),0)
charnum = -1;
else
A = fileread(fname);
char_1 = strfind(A, character);
charnum = numel(char_1);
if isequal(exist(fname),0)
charnum = -1;
elseif isequal(charnum,0)
charnum = -1;
elseif isequal(ischar(char(character)),0)
charnum = -1;
end
end
end
Thank you all for your suggestions and advice.
I understand, that I probably should add an instruction to take into account all the characters but I can't find a satisfying way that works. Because when I test for the character '#', it works correctly on my MATLAB: When I test the function with the visible character '#' the answer is correct.

採用された回答

Walter Roberson
Walter Roberson 2019 年 4 月 12 日
"or character is not a valid char" is a requirement to test whether the second parameter is datatype char .
Your code is testing isequal(charnum,0) which is testing whether the number of entries found by strfind() is 0, and when it is then you return -1. However, when the input is valid as char data type but does not happen to be found, then input is not an invalid character and so the output should not be -1: the output should be the count, which happens to be 0.
  2 件のコメント
Morgane Aubineau
Morgane Aubineau 2019 年 4 月 12 日
Thank you Walter Roberson. My code isqual... was indeed wrong in that way. But I still can't figure out what to do to pass the "Test with all visible characters". Because when I do it with the grader it's wrong (returns -1 apparently) but sounds right when testing on MATLAB. If I put a text file with "#" as in the example, it returns the number of "#" correctly. It puzzles me. Have you an idea why? Or what can I do to fix it?
Walter Roberson
Walter Roberson 2019 年 4 月 12 日
No, if they passed in a character, any character, then as long as the file is found, then you should not be returning -1. -1 is only for the case where the file is not found or the input does not pass ischar() . In all cases where a char was passed in but the char does not happen to occur in the file, then 0 should be returned.

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

その他の回答 (2 件)

Gokul surya Subramanian
Gokul surya Subramanian 2019 年 4 月 17 日
if ischar(character)==0
charnum =-1;
elseif charnum==0
charnum=0;
return;
end

Morgane Aubineau
Morgane Aubineau 2019 年 4 月 17 日
I finally solved it like this:
function charnum = char_counter(fname, character)
if isequal(isfile(fname),0)
charnum = -1;
elseif isequal(ischar(character),0)
charnum = -1;
else
A = fileread(fname);
characters = char(character);
char_1 = strfind(A, characters);
charnum = numel(char_1);
if isequal(exist(fname),0)
charnum = -1;
elseif isequal(charnum,0)
charnum = 0;
elseif isequal(ischar(char(character)),0)
charnum = -1;
end
end
end
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 4 月 18 日
It is not necessary to use if isequal(isfile(fname),0) : you could just use if ~isfile(fname)
If isfile(fname) fails, then you never get to the else and so you do not need to test exist(fname) . Likewise, you cannot get to that second ischar() test.

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

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by