I have problem with using fopen

31 ビュー (過去 30 日間)
Ahmed Al-Noori
Ahmed Al-Noori 2015 年 6 月 12 日
コメント済み: Jan 2015 年 6 月 14 日
hello I try to create htk file using the following code
function ht_kwrite(filename, data, frate, feakind)
[ndim, nframes] = size(data);
filid=fopen(filename,'wb','ieee-be');
fwrite(filid,nframes,'int32');% number of frames
fwrite(filid, frate, 'int32'); % frame rate in 100 nano-seconds unit
fwrite(filid, 4 * ndim, 'short'); % 4 bytes per feature value
fwrite(filid, feakind, 'short'); % 9 is USER
fwrite(filid, data, 'float');
fclose(filid);
the problem is with using fopen when create file which always return filid=-1 (the file can not created )I dont know where the problem?

回答 (2 件)

Konstantinos Sofos
Konstantinos Sofos 2015 年 6 月 12 日
編集済み: Konstantinos Sofos 2015 年 6 月 12 日
Hi Ahmed,
Have you read the documentation for the function fopen ?
fileID = fopen(filename,permission,machinefmt,encodingIn) additionally specifies the order for reading or writing bytes or bits in the file using the machinefmt argument. The optional encodingIn argument specifies the character encoding scheme associated with the file.
what is the wb in your code?
Please try
filid=fopen(filename,'w','ieee-be');
or
filid=fopen(filename,'w','b');
In addition you can find an htk write function by Kamil Wojcicki in file exchange.
Regards

Walter Roberson
Walter Roberson 2015 年 6 月 14 日
Using 'wb' is accepted as being equivalent to 'w' for compatibility with C programming.
The common reason for not being able to open a file for writing is that you do not have permission to write to the directory or file. You should be checking the result of the fopen()
[fid, errmsg] = fopen(filename, 'w', 'ieee-be');
if fid < 0
error(sprintf('failed to open "%s" because "%s"', filename, errmsg));
end
  1 件のコメント
Jan
Jan 2015 年 6 月 14 日
Or without sprintf:
error('failed to open "%s" because "%s"', filename, errmsg);

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by