MATLAB: Does MATLAB 7.7 (R2008b) give me an “invalid file identifier” error when I execute the FWRITE function

MATLAB

I am trying to open a file for writing using a command as follows:
fid = fopen('myfile')
However, when I use the FWRITE function as follows:
fwrite(fid,data)
MATLAB gives me an error message:
fid =
-1
??? Error using ==> fwrite
Invalid file identifier.
Error in ==> ConvertAndFileBiasData at 6
fwrite(fid,data)

Best Answer

The following command,
fid = fopen('myfile');
will (by default) open a file for reading data. If the file does not exist, then the FOPEN function returns '-1' indicating that the file was not found. The above command may still work as a result of having a file called 'myfile' in your current directory or MATLAB search path.
To execute your code, open your file using the syntax as follows:
fid = fopen('myfile','w')
This will open your file in "write" mode and will create a new file if a file called 'myfile' does not already exist. For more information about setting file permissions for the FOPEN function, please refer to the following online resource: