Error using textscan Invalid file identifier. Use fopen to generate a valid file identifier.
18 views (last 30 days)
Show older comments
Hi i am using GMM to train the system and the given folder contains audio files but i keep getting the error.My code is
% set paths to the wave files and protocols
pathToDatabase = fullfile('..','ASVspoof2017_train','wav');
trainProtocolFile = fullfile('..','ASVspoof2017_train', 'protocol', 'ASVspoof2017_train.trn');
devProtocolFile = fullfile('..','ASVspoof2017_train', 'protocol', 'ASVspoof2017_dev.trl');
% read train protocol
fileID = fopen(trainProtocolFile);
protocol = textscan(fileID, '%s%s%s%s%s%s%s');
fclose(fileID);
and my files are in the same directory as the matlab script file.
2 Comments
KSSV
on 11 Sep 2017
trainProtocolFile this contains multiple files...you need to send one by one in a loop.
Walter Roberson
on 11 Sep 2017
No, trainProtocolFile would end up referring to a single file in the above code.
"my files are in the same directory as the matlab script file"
Your code does not look for them there. Your code looks for them in a directory one level up. You are constructing the name
../ASVspoof2017_train/protocol/ASVspoof2017_train.trn
which looks up one level for the ASVspoof2017_train directory.
Answers (1)
OCDER
on 11 Sep 2017
Edited: OCDER
on 11 Sep 2017
mfilePath = fileparts(mfilename('fullpath')); % path to script file
pathToDatabase = fullfile(mfilePath, 'ASVspoof2017_train'); % mfilePath/ASVspoof2017_train
pathToWavFiles = fullfile(pathToDatabase, 'wav'); % mfilePath/ASVspoof2017_train/wav
trainProtocolFile = fullfile(pathToDatabase, 'protocol', 'ASVspoof2017_train.trn');
devProtocolFile = fullfile(pathToDatabase, 'protocol', 'ASVspoof2017_dev.trl');
% read train protocol
fileID = fopen(trainProtocolFile);
if fileID < 0 %catch fopen error
error('Could not open "%s".', trainProtocolFile);
end
protocol = textscan(fileID, '%s%s%s%s%s%s%s');
fclose(fileID);
Using ".." is troublesome if calling your script in a different working folder. Get the path where the matlab script is instead if it's in the same folder, and use this as the reference folder.
mfilePath = fileparts(mfilename('fullpath'));
Check this too - you had:
pathToDatabase = fullfile('..','ASVspoof2017_train','wav');
Is this a wav folder storing wav files ASVspoof2017_train/wav, or a single wav file ASVspoof2017_train.wav? I assumed the former and renamed it as pathToWavFiles so that you can use pathToDatabase as the ASVspoof2017_train folder.
Lastly, catch the error after fopen just so you get the relevant error message.
if fileID < 0
error('Could not open "%s".', trainProtocolFile);
end
2 Comments
Stephen23
on 11 Sep 2017
Edited: Stephen23
on 11 Sep 2017
Even better is to get the actual message from fopen, which gives extra useful information about why the file could not be opened:
[fileID,msg] = fopen(trainProtocolFile);
assert(fileID>=3,msg)
and also note that
- one assert is simpler than if and error, and
- both error and assert also support sprintf-style syntaxes:
thus we can simply do this:
assert(fileID>=3,'Could not open file (%s):\n%s',msg,trainProtocolFile)
There is no point in calling sprint separately (in fact using sprintf inside either error or assert is marked by the MATLAB Editor code checker).
OCDER
on 11 Sep 2017
assert is pretty nice! But fopen error message can sometimes be non-descriptive...
No such file or directory
which is why I made that error message to see the file name that was used. Best of both worlds?
[fileID, msg] = fopen(trainProtocolFile);
assert(fileID >= 3, sprintf('Could not open "%s"\n%s, trainProtocolFile, msg));
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!