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.
Related
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).
MATLAB provides the mfilename function. It returns the name of the file where the function was invoked, but unfortunately, it returns the file name without the extension.
So for example, if we have a file called myfile.m and we call mfilename inside the file, it will return the string 'myfile' but not 'myfile.m'
I also had a look at the fileparts function but it is not useful either because it only parses the string that you provide.
I am developing a piece of code has a different behavior based on the file extension. So for instance, it needs to know whether the extension of the file is .m or .p at run time.
You can check the list of extensions associated with MATLAB here.
How can I do it?
Looking at the docs, it seems like you can get the information you need from the dbstack command, it will take some minor additional processing though.
[ST, I] = dbstack('-completenames', 1)
ST =
file: 'C:\myProject\myfile.m'
name: 'myfile'
line: 2
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.
i am still working at my project and i again i hit a wall.
In this case i am trying to open a .pdf file from uimenu, i have searched through functions, i think i found the right one:
x = 'D:\MATLAB\Author.pdf';
y = 'D:\MATLAB\Bibliography.pdf';
f=uimenu('Label','ProjectData');
uimenu(f,'Label','Author','Callback','fopen(x)');
uimenu(f,'Label','Bibliography','Callback','fopen(y)');
uimenu(f,'Label','Close','Callback','close',...
'Separator','on','Accelerator','Q');
But the problem is that when i click on author or bibliography nothing happens, no errors, nothing.
The only thing that appears in command window is this :
ans =
-1
The .pdf files are in the same folder as the rest of the .m files.
Please help, Thank you !
That's because MATLAB can't open your file.
From the fopen documentation (available here) (the bold font is from me):
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.
So MATLAB is giving this fileID of -1. If you want MATLAB to open the appropriate application for your pdf file (i.e. Adobe Acrobat), you can use open.
If you want to actually open pdf as images in MATLAB, there is probably some function on the File Exchange.
Hope that helps!
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.