FSEEK error while running Matlab function
6 次查看(过去 30 天)
显示 更早的评论
I got this error while running a function in Matlab
"Error using fseek Invalid file identifier. Use fopen to generate a valid file identifier."
The overall command of the function is:
function [global_mean_vec, global_var_vec, total_fr_no]=global_mean_var_for_hmm_skips_1gau(traininglist_filename,model_filename_old, model_filename_new)
if nargin ==0
traininglist_filename='training_list.mat';
model_filename_old='hmm_with_skips.mat';
model_filename_new='hmm_with_skips_new.mat';
end;
load(model_filename_old, 'mean_vec_i_m', 'var_vec_i_m', 'a_i_j_m');
load(traininglist_filename,'list');
[dim,N,MODEL_NO]=size(mean_vec_i_m);
% allocate mean vectors of states of models
vector_sum=zeros(dim,1);
vector_squared_sum=zeros(dim,1);
total_fr_no = 0;
utterance_no=size(list,1);
for k=1:utterance_no
filename=list{k,2};
m=list{k,1}; % word ID
fid=fopen(filename,'r');
fseek(fid, 12, 'bof'); % skip the 12-byte HTK header
c=fread(fid,'float','b');
fclose(fid);
fr_no=length(c)/dim;
c=reshape(c,dim,fr_no);
total_fr_no = total_fr_no + fr_no;
vector_sum=vector_sum+sum(c,2);
vector_squared_sum=vector_squared_sum+sum(c.*c,2);
end %for k=1:utterance_no
global_mean_vec=vector_sum/total_fr_no;
global_var_vec=vector_squared_sum/total_fr_no - global_mean_vec.*global_mean_vec;
% model initilizatiion
for m=1:MODEL_NO
for i=2:N-1;
mean_vec_i_m(:,i,m) = global_mean_vec;
var_vec_i_m(:,i,m)= global_var_vec;
end
end
save(model_filename_new, 'mean_vec_i_m', 'var_vec_i_m', 'a_i_j_m');
fprintf('%s initialized with global mean vector and variance vector\n', model_filename_new);
0 个评论
回答(1 个)
dpb
2017-7-26
The error says the file handle is invalid --
...
fid=fopen(filename,'r');
fseek(fid, 12, 'bof'); % skip the 12-byte HTK header
You don't check the result of fopen so one must presume that the call failed...are the desired files in the CWD or in a subdirectory on the MATLABPATH? Not being is a common failure cause.
[fid,message]=fopen(filename,'r');
if fid<0, error(message), end
will let you know what happened and the cause.
3 个评论
Walter Roberson
2017-7-28
Are the loaded file names fully qualified, drive and directory and all? Or are they using relative paths? If they are relative then have you checked that you are cd to the directory you think you are using? Have you told File Explorer to show file extensions and checked that they match the names being loaded?
Jan
2017-7-28
编辑:Jan
2017-7-28
@N Rh: Matlab tells you clearly and unequivocally, that the corresponding file does not exist. Therefore the claim "despite that all files exist" must be wrong. You are either looking in the wrong folder or there is a typo in the names. Trust Matlab. When it cannot find a file, it is not there.
You can expand the error message:
error(['Missing file: %s', char(10), ...
'Current folder: %s', char(10), ...
'Message: %s'], filename, cd, message);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!