MATLAB: Strange behaviour of fwrite

freadfwrite

I tried to run the example provided on the documentation page for fread (uk.mathworks.com/help/matlab/ref/fread.html)
>> fileID = fopen('nine.bin','w');
>> fwrite(fileID,[1:9],'uint16');
Error using fwrite
Invalid file identifier. Use fopen to generate a valid file identifier.
Any idea why the example does not run on my installation?

Best Answer

FileName = 'nine.bin';
[fileID, Msg] = fopen(FileName, 'w');
if fileID == -1
error(['Cannot open file: %s', char(10), '%s'], FileName, Msg);
end
Most likely you do not have write permissions in the current folder.