MATLAB: Invalid file identifier despite of permissions - matlab

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.

Related

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?

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.

Getting a type error when attempting to move a file

I'm trying to move a file into a folder with this code
function addImage_Callback(hObject, eventdata, handles)
% get the file to add to another folder
[baseName, folder] = uigetfile();
% get the full file path of the file to add
fullFilePath = fullfile(folder, baseName);
% get the full folder path of the folder currently selected in the listbox
selectedFolder = get(handles.folders,'string');
selectedFolder = selectedFolder{get(handles.folders,'value')};
% move the file to the folder
moveFile(fullFilePath, selectedFolder);
And receiving this error
Undefined function 'moveFile' for input arguments of type 'char'.
Error in niceinterface>addImage_Callback (line 549)
moveFile(fullFilePath, selectedFolder);
Does anyone know what's going on? The matlab spec for movefile says that it takes strings and the code that I've used to get the folders/files has worked previously.
After 'disp'ing them I received the following output
D:\Downloads\44d9de8db7c840bf0f3fabc9371f9e0d.jpeg
D:\ImageViewer
Thanks all!

MATLAB - read files from directory?

I wish to read files from a directory and iteratively perform an operation on each file. This operation does not require altering the file.
I understand that I should use a for loop for this. Thus far I have tried:
FILES = ls('path\to\folder');
for i = 1:size(FILES, 1);
STRU = pdbread(FILES{i});
end
The error returned here suggests to me, a novice, that listing a directory with ls() does not assign the contents to a data structure.
Secondly I tried creating a file containing on each line a path to a file, e.g.,
C:\Documents and Settings\My Documents\MATLAB\asd.pdb
C:\Documents and Settings\My Documents\MATLAB\asd.pdb
I then read this file using the following code:
fid = fopen('paths_to_files.txt');
FILES = textscan(fid, '%s');
FILES = FILES{1};
fclose(fid);
This code reads the file but creates a newline where a space exists in the pathway, i.e.
'C:\Documents'
'and'
'Setting\My'
'Documents\MATLAB\asd.pdb'
Ultimately, I then intended to use the for loop
for i = 1:size(FILES, 1)
PDB = pdbread(char(FILES{i}));
to read each file but pdbread() throws an error proclaiming that the file is of incorrect format or does not exist.
Is this due to the newline separation of paths when the pathway file is read in?
Any help or suggestions greatly apppreciated.
Thanks,
S :-)
First Get a list of all files matching your criteria:
( in this case pdb files in C:\My Documents\MATLAB )
matfiles = dir(fullfile('C:', 'My Documents', 'MATLAB', '*.pdb'))
Then read in a file as follows:
( Here i can vary from 1 to the number of files )
data = load(matfiles(i).name)
Repeat this until you have read all your files.
A simpler alternative if you can rename your files is as follows:-
First save the reqd. files as 1.pdb, 2.pdb, 3.pdb,... etc.
Then the code to read them iteratively in Matlab is as follows:
for i = 1:n
str = strcat('C:\My Documents\MATLAB', int2str(i),'.pdb');
data = load(matfiles(i).name);
% use our logic here
% before proceeding to the next file
end
I copy this from yahoo answers! It worked for me
% copy-paste the following into your command window or your function
% first, you have to find the folder
folder = uigetdir; % check the help for uigetdir to see how to specify a starting path, which makes your life easier
% get the names of all files. dirListing is a struct array.
dirListing = dir(folder);
% loop through the files and open. Note that dir also lists the directories, so you have to check for them.
for d = 1:length(dirListing)
if ~dirListing(1).isdir
fileName = fullfile(folder,dirListing(d).name); % use full path because the folder may not be the active path
% open your file here
fopen(fileName)
% do something
end % if-clause
end % for-loop