Matlab - error using textscan (invalid file identifier) - matlab

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?

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.

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

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).

Abaqus *.inp file created using Matlab

I was trying to do parametric studies in ABAQUS. I created an *.inp file (master) using GUI in abaqus, then wrote a matlab code to create a new *.inp file using the master. Master *.inp file can be found here and will be required to run the code.
In the new *.inp file everything was same as the master except a few specific lines which I am changing for parametric studies, code is given below. I am getting the files nicely but the problem is ABAQUS can't read the file and gives error messages. By visual inspection I don't find any faults. I guess matlab is writing the *.inp file in some other format which ABAQUS can't interpret.
clc;
%Number of lines to be copied
total_lines=4538; %total number of lines
lines_b4_RP1=4406; % lines before reference point 1
%creating new files
for A=0
for R=[20 30 40 50 100 200 300 400 500]
fileroot = sprintf('P_SHS_120X120X1_NLA_I15_A%dR%d.inp', A,R);
main_inp=fopen('P_SHS_120X120X1_NLA_I15_A0R10.inp','r'); %inputting the main inp file to be copied
wfile=fopen(fileroot,'w+'); %wfile= writing the new file
for i=1:total_lines
data=fgets(main_inp);
if i<lines_b4_RP1
fprintf(wfile,'%s\n', data);
elseif i==lines_b4_RP1
formatline1=('%s\n');
txtline='*Node';
fprintf(wfile, formatline1 ,txtline);
elseif i==(lines_b4_RP1+1)
formatline2=('%d%s%d%s%d%s%d\r\n');
comma=',';
refpt1=1;
xcoord1=R*cosd(A);
ycoord1=R*sind(A);
zcoord1=-20;
fprintf(wfile, formatline2, refpt1,comma,xcoord1,comma,ycoord1,comma,zcoord1);
elseif i==(lines_b4_RP1+2)
fprintf(wfile, formatline1 ,txtline);
elseif i==(lines_b4_RP1+3)
refpt2=2;
xcoord2=R*cosd(A);
ycoord2=R*sind(A);
zcoord2=420;
fprintf(wfile, formatline2 ,refpt2,comma,xcoord2,comma,ycoord2,comma,zcoord2);
elseif i>(lines_b4_RP1+3)
fprintf(wfile,'%s\n', data);
else break;
end
end
fclose(main_inp);
fclose(wfile);
end
end
Thanks in advance.
N.B. A sample *.dat file containing the error message is given here.
You are using fgets to get each line of the input file. From the matlab help:
fgets: Read line from file, keeping newline characters
You then print each line using
fprintf(wfile,'%s\n', data);
This creates two newlines at the end of each data line in the file. A second problem in your file is that you use \r\n in your format specifier. In matlab (unlike C) this will give you two newlines. e.g.
>> fprintf('Hello\rWorld\nFoo\r\nBar\n')
Hello
World
Foo
Bar
>>
I would suggest in future to test this approach with a much simpler format that you can use. Also there is a
*preprint
option that allows you to echo the contents of the input file back into the dat file. This creates big dat files, but it is useful for debugging.

MATLAB: Use a variable with a file extenstion to load files

I am using MATLAB
I Have 51 files in their own directory all of .out extention created by a seperate program, all numbered 0 to 50.
ie
0.out
1.out
2.out
and so on til 50.out.
I need to load each file 1 by one to do calculations upon them within a for loop. How would I do this using the count variable to load the file, if the directory is set beforehand?
i.e.
%set directiory
cd(......)
%for loop
For count = 0:50,
data = count.out *<-----this line*
.....
Many thanks!
First generate the file name with
fileName = [int2str(count) '.out'];
then open the file with
fid = fopen(fileName, 'r');
The loading phase depends on the kind of file you want to read. Assuming it is a text file you can, for example, read it line after line with
while ~feof(fid)
line = fgetl(fid);
end
or use more specialized functions (see http://www.mathworks.it/it/help/matlab/text-files.html). Before the end of the for loop you'll have to close the file by calling
fclose(fid);
Another quite nice way to do it is to use the dir function
http://www.mathworks.co.uk/help/matlab/ref/dir.html
a = dir('c:\docs*.out')
Will give you a structure containing all the info about the *.out files in the directory you point it to, (or the path). You can then loop through it bit by bit. using fopen or csvread or whatever file reading function you want to use.

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'];