Why using "fullfile" inside "fopen" gives error but "uigetfile" along with "fopen" works fine?

4 visualizzazioni (ultimi 30 giorni)
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--
  1. https://www.mathworks.com/help/matlab/ref/fullfile.html
  2. https://www.mathworks.com/help/matlab/ref/fopen.html
  3. https://www.mathworks.com/matlabcentral/newsreader/view_thread/88964
  4. https://www.mathworks.com/matlabcentral/answers/73662-open-multiple-files-with-fopen
  5. http://stackoverflow.com/questions/10606373/what-causes-an-invalid-file-identifier-in-matlab
  4 Commenti
Steven Lord
Steven Lord il 12 Gen 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.
Sugato
Sugato il 12 Gen 2017
After diagnosing a little bit more, I realized that there was a minor error in the filename ( variable: filename) that was provided by hardcoding it. The uigetfile on the other hand was able to pick the file name and path correctly and that's why it was working. Thank you very much for your time and suggestions.
@Adam: Thanks for pointing out where the error could be.

Accedi per commentare.

Risposta accettata

Jan
Jan il 12 Gen 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.

Più risposte (1)

Walter Roberson
Walter Roberson il 12 Gen 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)

Categorie

Scopri di più su MATLAB Compiler in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by