MATLAB: Text file i/o problem….I always get 0 with charnum …whats the problem with the code?

text file

function charnum=char_counter(fname,character)
fid=fopen(fname,'rt')
if fid<0
charnum=-1;
return;
end
if fid ~=0 && ischar(character)==true
charnum=count(fname,"character ");
else
charnum=-1;
end

Best Answer

Your code has likely has several bugs, e.g. you were checking for literal "character" as opposed to using the contents of the character variable:
charnum = count(fname,character);
One more bug is that you are checking the input fname for these characters... but I suspect that you need to import the file contents and check that, but nowhere in your code do you actually import the file contents.