Error using fread Invalid file identifier. Use fopen to generate a valid file identifier - matlab

I have a code of a function given by my professor. In his Matlab these function works, but in mine generate these error:
''Error using fread Invalid file identifier. Use fopen to generate a
valid file identifier.
Error in funcrion (line 18) A = fread(fileID ,inf, 'uint8');'''
I think that the problem is the Matlab path, however, I set it before, and I'm sure that is correct. For safety, I add both folders with functions than folders with data.
Thank you

Related

Matlab fopen error with csv file

I'm just trying to import a csv file into Matlab using the textscan function. But everytime i run the program it always throws back this error
Error using textscan
Invalid file identifier. Use fopen to generate a
valid file identifier.
But as you can see in the code below i'm using fopen to prepare the file for the use of textscan.
S = 'Proto2.csv';
fidi = fopen(S);
C = textscan(fidi, '%f%s%f%f%f%f%f%f%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f', 'Delimiter','\n', 'HeaderLines',11, 'CollectOutput',1);
Afterwards i'm using C to get access to data i need out of the csv file
Normally, before proceeding with reading and/or writing operations on a file handle, you must make sure that the handle returned by the fopen function is valid. From the official documentation of Matlab:
fileID = fopen(filename) opens the file, filename, for binary read
access, and returns an integer file identifier equal to or greater
than 3. MATLAB® reserves file identifiers 0, 1, and 2 for standard
input, standard output (the screen), and standard error, respectively.
If fopen cannot open the file, then fileID is -1.
Check your fidi variable value before proceeding with textscan, I'm pretty sure it is equal to -1. This happens either because the file is not found (if you don't specify a full absolute path, Matlab searches for it within the current working directory) or because the file has a sharing lock on it (hence, make sure that it's not being used by other applications while you attempt to read it).

Read wave file in matlab

this is the code and i have no idea why it's not workinng
y=audioread('Arabic_Male_Alain_G_Corp');
Error using audioread (line 74)
The filename specified was not found in the MATLAB path.
Is this file located in your active folder? If not, you need to specify the full filepath (e.g. C:\...)

How to open a .p file with fopen in Matlab

I have an third-party file with the extension .p and I try if it is possible to open it using the function fopen:
fopen(title.p)
This returns 3, which is a file identifier according to the help function:
help fopen
fopen - Open file, or obtain information about open files
This MATLAB function opens the file, filename, for binary read access, and returns an integer file identifier equal to or greater than 3.
What should I do to see (if possible) the content of the file?
A file with the extension .p in the context of matlab probably refers to a pcode file. It is obfuscated bytecode, you won't be able to read anything useful. Nevertheless you can run the function calling it like any other matlab function.
I was able to open the file using
fid = fopen('file.p')
fgetl(fid)
However, the content was not useful, indeed.

Error while using fopen saying 'Inavlid file identifier' [duplicate]

I have a MATLAB script that I could have sworn worked fine the last time I used it (a year ago). Now, I get this error:
Invalid file identifier. Use fopen to generate a valid file identifier.
If I understand correctly, it is failing to find or open(?) a file specified elsewhere in the script. Is this right? If so, what could cause it?
fid (file identifier) is the output of fopen. It's an integer, but not related to the file permanently. You need to use fopen to get the fid. It seems to me that you are using incorrect fid (file identifier) in some file-related I/O command, such as fread, fscanf or fclose. Unsuccessful fopen gives fid of -1. For any valid normal file successful fopen will give fid that is 3 or greater integer.
However, without any code it's impossible to say where or what the bug or error is. You could use MATLAB debugger to single-step the code from relevant fopen (set breakpoint there and run your program) until the relevant fclose and see if fid (or whatever variable name you use for file identifier) or any data structure for your file identifiers (if have more than one file identifier in your code) changes in any point between relevant fopen and fclose.
I solved this problem for my self by adding permission option to fopen.
As you see in http://www.mathworks.se/help/matlab/ref/fopen.html , fopen syntax is:
fileID = fopen(filename,permission)
Possible permissions, for example are:
'r' (default) | 'w' | 'a' | 'r+' | 'w+' | 'a+' | ...
'r' – Open file for reading.
'w' – Open or create new file for writing. Discard existing contents, if any.
'a' –
Open or create new file for writing. Append data to the end of the file.
'r+' – Open file for reading and writing.
'w+' – Open or create new file for reading and writing. Discard existing contents, if any.
'a+' – Open or create new file for reading and writing. Append data to the end of the file.
...
If I use fopen without permission option, or if I use 'r' (default) option, fopen will return -1, which is error. I success with this:
fid=fopen('tmp.txt', 'w');
fid=fopen('tmp.txt', 'a');
I had this problem. It turned out that the file I was trying to write was too large (I didn't have enough free space to accommodate it). However, the program didn't fail until the call to fclose. Very confusing!
Try freeing up some space, or writing a very small file, to test this diagnosis.
I encountered the same problem when trying to open ASF toolbox demos. Running Matlab as an administrator(right-click to open) seemed to solve this issue for me.
fopen can fail because MATLAB doesn't have the permissions to read/write the file you've specified.
Try opening a file in a location where you/MATLAB have all the rights (depending on your OS).
I have used fopen with permission and the same error came out. However, I started MATLAB as admin and that took care of the problem.
I had the file opened in excel and as a result fopen returned a -1.
Took me forever to find such a trivial problem.
It also happens when trying to create a file in a non-existent directory. Try mkdir('folderName') within MATLAB or just create the directory beforehand.
The path with a forward slash at the beginning can cause the same error.
filename = '/data/myfile.txt';
throws this error, while
filename = 'data/myfile.txt';
does not produce an error.
For my situation, I have checked everything, but missed an easy step.
Please select "browse your folder" and browse for your current document location before you run your 'fopen' code.
It also occurs when a script is trying to read beyond the end of the file.

What causes an invalid file identifier in MATLAB?

I have a MATLAB script that I could have sworn worked fine the last time I used it (a year ago). Now, I get this error:
Invalid file identifier. Use fopen to generate a valid file identifier.
If I understand correctly, it is failing to find or open(?) a file specified elsewhere in the script. Is this right? If so, what could cause it?
fid (file identifier) is the output of fopen. It's an integer, but not related to the file permanently. You need to use fopen to get the fid. It seems to me that you are using incorrect fid (file identifier) in some file-related I/O command, such as fread, fscanf or fclose. Unsuccessful fopen gives fid of -1. For any valid normal file successful fopen will give fid that is 3 or greater integer.
However, without any code it's impossible to say where or what the bug or error is. You could use MATLAB debugger to single-step the code from relevant fopen (set breakpoint there and run your program) until the relevant fclose and see if fid (or whatever variable name you use for file identifier) or any data structure for your file identifiers (if have more than one file identifier in your code) changes in any point between relevant fopen and fclose.
I solved this problem for my self by adding permission option to fopen.
As you see in http://www.mathworks.se/help/matlab/ref/fopen.html , fopen syntax is:
fileID = fopen(filename,permission)
Possible permissions, for example are:
'r' (default) | 'w' | 'a' | 'r+' | 'w+' | 'a+' | ...
'r' – Open file for reading.
'w' – Open or create new file for writing. Discard existing contents, if any.
'a' –
Open or create new file for writing. Append data to the end of the file.
'r+' – Open file for reading and writing.
'w+' – Open or create new file for reading and writing. Discard existing contents, if any.
'a+' – Open or create new file for reading and writing. Append data to the end of the file.
...
If I use fopen without permission option, or if I use 'r' (default) option, fopen will return -1, which is error. I success with this:
fid=fopen('tmp.txt', 'w');
fid=fopen('tmp.txt', 'a');
I had this problem. It turned out that the file I was trying to write was too large (I didn't have enough free space to accommodate it). However, the program didn't fail until the call to fclose. Very confusing!
Try freeing up some space, or writing a very small file, to test this diagnosis.
I encountered the same problem when trying to open ASF toolbox demos. Running Matlab as an administrator(right-click to open) seemed to solve this issue for me.
fopen can fail because MATLAB doesn't have the permissions to read/write the file you've specified.
Try opening a file in a location where you/MATLAB have all the rights (depending on your OS).
I have used fopen with permission and the same error came out. However, I started MATLAB as admin and that took care of the problem.
I had the file opened in excel and as a result fopen returned a -1.
Took me forever to find such a trivial problem.
It also happens when trying to create a file in a non-existent directory. Try mkdir('folderName') within MATLAB or just create the directory beforehand.
The path with a forward slash at the beginning can cause the same error.
filename = '/data/myfile.txt';
throws this error, while
filename = 'data/myfile.txt';
does not produce an error.
For my situation, I have checked everything, but missed an easy step.
Please select "browse your folder" and browse for your current document location before you run your 'fopen' code.
It also occurs when a script is trying to read beyond the end of the file.