how to fix fread error ?
조회 수: 14 (최근 30일)
이전 댓글 표시
Hello when l run the code above l got this error . l don't where is the error, my codes (.m) and training examples are in the same working directory.
*Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in MNISTParser (line 3)
magic_number = fread(fp,1,'uint32',0,'b');
Error in MNIST_gen (line 2)
train_labels = MNISTParser('./MNIST/train-labels-idx1-ubyte');*
code 1 : MNIST_gen.m
...
**train_labels = MNISTParser('./MNIST/train-labels-idx1-ubyte');**
...
[Irrelevant code elided for brevity...dpb]
code2: MNISTParser.m
function res = MNISTParser(filename)
fp = fopen(filename,'r');
**magic_number = fread(fp,1,'uint32',0,'b');**
items_number = fread(fp,1,'uint32',0,'b');
...
[ditto]
Thanks for helps
댓글 수: 0
채택된 답변
dpb
2016년 3월 29일
In function MNISTParser
fp = fopen(filename,'r');
magic_number = fread(fp,1,'uint32',0,'b');
you didn't test the return value from fopen. The problem is that the file './MNIST/train-labels-idx1-ubyte' was not successfully opened before the subsequent attempt to read from it. A plausible reason could be that the use of the relative path is not correct and the file isn't actually where that implies it must be. ALWAYS test the return value in code; it can help if you also return the optional error message and report it if it fails...
[fp,msg] = fopen(filename,'r');
if fp<1, error([msg ' File: ' filename]), end
...
would be a minimalist error check with the extra info on what the actual failure was.
댓글 수: 2
dpb
2016년 3월 30일
Well, to be clear it told you whatever the error in msg happened to be but for aid in determining where, I added the file name that was used for the call as well so knew which particular file it was that failed. It may or may not have been that there was something wrong in the name itself.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!