Error using fopen - matlab

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

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.

Index out of bounds after reading a text file

I have the following simple code, and I tried to use one of the indices from the .txt file. The index that I want is at (4,1) while the size of my matrix in the .txt file is (8,4). When I run the code, MATLAB give me the following error;
Attempted to access q(4,1); index out of
bounds because size(q)=[1,601]
Can someone help me understand why I receive the error and how to fix it?
Here is the code:
q = fileread('sv11edit.txt');
toe = q(4,1)
The answer will depend on the format of the file sv11edit.txt. However, fileread returns a string of characters. In this case, it gives you a string that is 601 characters long. You receive an error because you assume that q is 8 by 4, but this is not the case.
Check what is being stored in q before you try anything like the second line of your code. The function load may be a better alternative to fileread.

Using datestr(now) with save

The code is:
filename = sprintf('michael%s.bat',datestr(now));
...
save (filename,vec)
vec is a vector
I'm getting this error:
Error using save
Argument must contain a string.
Error in sumfnc (line 13)
save (filename,vec)
I'm unsure on how filename is not a string.
The problem is not filename, it is vec. With the functional usage of save, you need to do:
save(filename,'vec')
However, since filename will contain a space, you will also need to modify filename. Try:
save(strrep(filename,' ','_'),'vec')
to replace spaces with _.

MATLAB Saving Figure Invalid Filename Error

I am writing a program to plot graphs in a loop and I want to save each graph that comes out as a .jpg file with a variation of the file name. Here is my code for saving the graphs:
filename = strcat('WI_Pollutants_', D(i,6), '_200706_O3');
saveas(gcf, filename, 'jpg');
The saved file should come out as the following with D(i,6) changing each iteration of the loop.
WI_Pollutants_003-0010_200706_O3.jpg
However, I'm running an error: (Maybe it has to due with saveas wanting a string only?)
Error using saveas (line 81)
Invalid filename.
saveas only accepts characters as the filename. But when filename was created, strcat made it a cell array. Therefore, the filename needs to be converted to a character array.
filename = char(strcat('WI_Pollutants_', D(i,6), '_200706_O3'));
saveas(gcf, filename, 'jpg');
This solves the problem.
I think your D{i,6} is ending up wrapped up as an array, from this line:
D{i,6} = [num2str(D{i,6}) '-' num2str(D{i,7})];
To solve this, just removing the []'s
D{i,6} = num2str(D{i,6}) '-' num2str(D{i,7});
I think what happened is your D{i,6}=['someString'], and concatinating added in the []'s that werent' desired.
As a check, if something like this happens again, just fprintf(filename) right before use, and look at what comes out. I suspect you'll find the issue there. You can always remove the print statement later on.

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