Why using "fullfile" inside "fopen" gives error but "uigetfile" along with "fopen" works fine?
4 views (last 30 days)
Show older comments
I am trying to use: Method-1
fullName1 = fullfile(filepath, filename);
[fid1, msg1] = fopen(fullName1 , 'r', 'ieee-le');
Method-1 yields:
fid1 = -1 (file was NOT found).
I have also tried doing this as follows: Method-2
[fName, fPath] = uigetfile(*.*);
fullName2 = [fPath, fName];
[fid2, msg2 = fopen(fullName, 'r', 'ieee-le');
Method-2 yields:
fid2 = 3 (file was opened).
This is so surprising! The first method could not find the file at all while the second could. So, I compared the strings fullName1 and fullName2:
strcmp(fullName1, fullName2) --> This yields 0;
=> fullName1 and fullName2 are different!
If someone could kindly explain why this is happening and what could fix this, that will help me a lot. My ultimate goal is to use fileName1 inside fopen. (i.e. use method-1)
Thanks.
Note: The folder where the data files are kept has also been added to the search path in MATLAB. Also, I have already looked at all the following sites--
- https://www.mathworks.com/help/matlab/ref/fullfile.html
- https://www.mathworks.com/help/matlab/ref/fopen.html
- https://www.mathworks.com/matlabcentral/newsreader/view_thread/88964
- https://www.mathworks.com/matlabcentral/answers/73662-open-multiple-files-with-fopen
- http://stackoverflow.com/questions/10606373/what-causes-an-invalid-file-identifier-in-matlab
4 Comments
Steven Lord
on 12 Jan 2017
You called fopen with two outputs in the first call, the one that failed. What were the contents of that second output? That should be a message from the system indicating why fopen was unable to open the file.
Accepted Answer
Jan
on 12 Jan 2017
There can be other reasons for a failing fopen:
- The number of open files exceeds the limit defined by the operating system. See fopen('all')
- The file is opened for writing from anywhere else.
- Typos or invisible characters in the file name.
0 Comments
More Answers (1)
Walter Roberson
on 12 Jan 2017
Are the strings the same length?
Is it possible that there is a non-printing character in the file name? Compare double(fullName1) to double(fullName2)
0 Comments
See Also
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!