MATLAB Answers

problem with the function "fread"

21 views (last 30 days)
Antonis Koutsis
Antonis Koutsis on 15 Jan 2021
Answered: Jeremy Hughes on 15 Jan 2021
i have to read a .txt file , given by my professor, and i wrote the following code
fid=fopen('shannon_odd.txt','r'); %open file
x=fread(fid,'*char'); %read file chars
binary = dec2bin(x,8); %char->binary
binf=binary_t(:)-'0'; %vector of binary bits of file
and when i run the program, this error came at command window:


Ive J
Ive J on 15 Jan 2021
Is your text file within current working directory?
Antonis Koutsis
Antonis Koutsis on 15 Jan 2021
it was the first thing that i had checked before i post this

Sign in to comment.

Answers (1)

Jeremy Hughes
Jeremy Hughes on 15 Jan 2021
The error indicates that fopen returned an invalid file ID.
Try this:
[fid,msg] = fopen(filename)
if fid < 0
More than likely, the file is not in the current working directory. try
That will show you if the file is being found, if it doesn't give a result, you'll need to provide the full path to the file.


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!