MATLAB: Invalid file identifier. Use fopen to generate a valid file identifier - matlab

I am trying to create a new text file called Table. I keep getting this error:
Invalid file identifier. Use fopen to generate a valid file identifier.
My code is:
fileID = fopen('Table.txt', 'a+');
fprintf(fileID, '%6d %12d\r\n', 'Prior', 'New');
fprintf(fileID, '%6d %12d\r\n', A);
fclose(fileID);

I was able to solve the problem by changing the path to a temporary directory using cd(tempdir).

Related

MATLAB: Invalid file identifier despite of permissions

I am having the following code:
%% Methods
function obj = loadDataGroundTruth(obj)
dataFile = [obj.Configuration.dir.datafiles,'.csv'] ;
% Open the data file
fid = fopen(dataFile,'r+'); %# open csv file for reading
% Scan data file until get to the segments part
bIdFound = false ;
while ((~feof(fid)) & (~bIdFound))
line = fgets(fid); %# read line by line
stringId = sscanf(line,'%s'); %# sscanf can read only numeric data :(
% Stop when we reach the trajectories section
if strcmp(stringId, 'Segments')
bIdFound = true ;
end
end
% Exit if there is not trajectories section in the file
if (~bIdFound)
% Close file descriptor
fclose(fid);
error('This data file does not contain motion information') ;
end
which is supposed to lead the .csv files and do some work with them. However, I get the error specified in the title. Full trace can be seen below:
Error using feof
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in vicon.DataLoaderVICON/loadDataGroundTruth (line 99)
while ((~feof(fid)) & (~bIdFound))
Error in vicon.DataLoaderVICON (line 82)
obj.loadDataGroundTruth() ;
Error in vicon.DataLoaderVICONEdges (line 39)
obj = obj#vicon.DataLoaderVICON(varargin{:})
Error in sequence.SequenceVICON (line 51)
obj.DataLoader = vicon.DataLoaderVICONEdges(directory,...
Error in Lshape0001 (line 21)
seq = sequence.SequenceVICON(SEQUENCE_NAME,...
The problem I suspect though is in this line:
while ((~feof(fid)) & (~bIdFound))
Before anyone tells me to do so: I've already launched Matlab 2017b with sudo rights (Ubuntu 16.04) and I've added the entire folder to the path (with its subfolders).
Thanks in advance.
Okay, figured out, thanks to contributions of user obchardon.
I've noticed that the path had somehow ./ included in it as well, which shouldn't have been the case. The file I was trying to run probably was written in a Windows machine, and thus had different symbols/notation in the path. Once I printed the path it was trying to load the file from, I've noticed that it should not have ./. I removed that from the path and it worked.

Error using fopen

I wrote these two lines to read and show an image from a specific location.
i = imread('‪C:\Users\m_mal\Desktop\fruit.jpg');
imshow(i);
But when I ran the code I got the following error messages.
Error using fopen
The file name contains characters that are not contained in the filesystem
encoding.
Certain operations may not work as expected.
Error in imread (line 343)
[fid,errmsg] = fopen(filename, 'r');
Error in g (line 1)
i = imread('?C:\Users\m_mal\Desktop\fruit.jpg');
Replace this:
i = imread('?C:\Users\m_mal\Desktop\fruit.jpg');
with this:
i = imread('C:\\Users\m_mal\Desktop\fruit.jpg');
The first character of the filename was a non-printing character (possibly a control character).Actually, I had copied and pasted URI from the properties of that image and included some non-printing character.
So, I deleted the URI and wrote it manually. Then the code worked.
You can see this character represented with '?' in the error message:
Error in g (line 1)
i = imread('?C:\Users\m_mal\Desktop\fruit.jpg');
That character was not visible but was there.
credit: Matlab community

Matlab - error using textscan (invalid file identifier)

I need to run a script in Matlab (2015b). The critical part of this script is:
%Create module to divide up files
%Import Matlab processed data from Gaus script
Gaus_import_Name= strcat('MvsL\MvsL_Combined_OutputGaus.csv');
Summary_gausian_infomration_Name=strcat('MvsL\MvsL_Summary_Gausians_for_individual_proteins.csv');
%import data files Gaus data
f1 = fopen (Gaus_import_Name);
Processed_data1 = textscan(f1, '%s', 'Delimiter',',');
fclose(f1);
"MvsL\MvsL_Combined_OutputGaus.csv" and "MvsL\MvsL_Summary_Gausians_for_individual_proteins.csv" are files that already exist and this script needs to open them. All files needed and the script are in the same folder. After running this script, I get this message:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Alignment (line 127)
Processed_data1 = textscan(f1, '%s', 'Delimiter',',');
I have also tried
f1 = fopen (Gaus_import_Name, 'r+');
but it did not help.
Do you have any experience with this kind of error?

Writing multiple output file matlab

I want to write output of code periodically in different files in some specific folder.
Code I am using is as follows:
for i=1:m
% some other things
if (mod(i,1000)==0)
y=[1:dx:n_x;c_initial.'];
fn = ['/home/alekhine' num2str(i) '.dat'];
fid=fopen(fn);
fprintf(fid,'%6.4f %12.8f\n',y);
fclose(fid);
end
end
But I am getting error from Matlab as follows
Error using ==> fprintf
Invalid file identifier. Use fopen to generate a valid file
identifier.
. What is wrong in the code? Any help will be appreciated.
You didn't give the permission argument to the fopen function, so it is opened for reading only.
See the docs (http://www.mathworks.com/help/techdoc/ref/fopen.html) for valid values for permission.
Just FYI, the preferred way of constructing filenames is to use FULLFILE. In this case, you could do
fn = fullfile('/home/alekhine', [num2str(i), '.dat']);
FULLFILE is preferred because it understands the different file separators on different OS types (i.e. \ on Windows and / on UNIX/Mac).
You seem to be missing a path separator in the path generation:
fn = ['/home/alekhine' num2str(i) '.dat'];
Should be:
fn = ['/home/alekhine/' num2str(i) '.dat'];

Why does fopen fail the first time, but work the second time?

I am using Matlab to create a new file by calling
fid = fopen(filename,'w')
since filename doesn't exist, it should create a new file and give me a valid file descriptor. Instead it returns -1. If I run the code again however, I get fid = 3.
This is being run on ubuntu but it apparently works fine on windows and I can't figure out why.
-Mike
not sure if this helps, but note that if the folder doesn't exist, fopen with 'w' can't create the file and so returns -1.
You should check out the two-output-argument form of fopen in the doc here. This allows you to do stuff like
[fh, failmessage] = fopen( fname, 'wt' );
if fh == -1
error( 'Failed to open %s: %s', fname, failmessage );
end