problem with the function "fread"

6 ビュー (過去 30 日間)
Antonis Koutsis
Antonis Koutsis 2021 年 1 月 15 日
回答済み: Jeremy Hughes 2021 年 1 月 15 日
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
binary_t=transpose(binary);
binf=binary_t(:)-'0'; %vector of binary bits of file
binf=binf.';
and when i run the program, this error came at command window:
  2 件のコメント
Ive J
Ive J 2021 年 1 月 15 日
Is your text file within current working directory?
Antonis Koutsis
Antonis Koutsis 2021 年 1 月 15 日
it was the first thing that i had checked before i post this

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

回答 (1 件)

Jeremy Hughes
Jeremy Hughes 2021 年 1 月 15 日
The error indicates that fopen returned an invalid file ID.
Try this:
[fid,msg] = fopen(filename)
if fid < 0
error(msg)
end
More than likely, the file is not in the current working directory. try
which('shannon_odd.txt')
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.
HTH

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by