Effacer les filtres
Effacer les filtres

Why can't MATLAB access a file that is in its current directory?

349 vues (au cours des 30 derniers jours)
Fahad
Fahad le 18 Août 2017
Commenté : Stephen23 le 26 Juin 2023
I am trying to execute a .m script that manipulates data in an Excel file. The MATLAB code and the Excel spreadsheet are in the same directory, which is also the current directory. However, whenever I click 'Run', an error message says that the file could not be found in the current directory or on the MATLAB path. I then went to File>Set Path, clicked 'Add Folder', browsed for the folder that was supposed to be my current directory and pressed Save. Afterwards, I tried again but to no avail. Can someone help me get the file to execute? I have already wasted a week trying to fix this problem. Thanking you in advance. P.S. I am using MATLAB R2008b.
  4 commentaires
Stephen23
Stephen23 le 18 Août 2017
NEVER save or alter files under the C:\Program Files directory.
That location is reserved for installed programs and apps on your computer. You should be working in your personal user directory.
Steven Lord
Steven Lord le 14 Nov 2019
There's another problem with those files. The ( and ) characters are not allowed in MATLAB script, function, or class names.

Connectez-vous pour commenter.

Réponses (3)

Guillaume
Guillaume le 18 Août 2017
Your screenshots clearly show that the current directory is C:\Program Files\MATLAB. The Program Files directory is a system directory on Windows. Under no circumstances should you put data files such as your m files and excel files in there. Matlab should have created a matlab directory for you in your My Documents folder. Work in there instead.
Recent versions of Windows, such as the one you're using, will actively prevent you putting data in Program Files to the point of lying to you. It may tell you that you've saved a file in there whereas it actually has saved it in a virtual store in another location. This may be the reason matlab can see the files in some circumstances and not others (depends on how it tries to access the files). So as said, put your code and excel file in My Documents
As to reading the Excel file, use the full path of the file instead of just the file name. It'll be more reliable, means the excel file doesn't have to be in the same directory as your code and means you don't have to manipulate matlab's path.
  1 commentaire
Miguel Mattis
Miguel Mattis le 14 Nov 2019
I've been having this exact problem, thank you so much

Connectez-vous pour commenter.


Aeyzechiah Vasquez
Aeyzechiah Vasquez le 2 Avr 2020
For anyone else having this issue, if the other answers didn't help, this might. Try doing
addpath .
This might show any problems with your current working directory. Your current working directory cannot be named resources. There may be other "forbidden" directory names, but this was mine. Matlab will not give you any warnings until you try to do this command.
Hope this helps someone!
  1 commentaire
Walter Roberson
Walter Roberson le 2 Avr 2020
resources became forbidden as a directory a small number of releases ago; it was in the release notes, but easy to overlook.

Connectez-vous pour commenter.


Nadine
Nadine le 26 Juin 2023
Unable to open file 'C:\Users\NK27504\Documents\MATLAB\internship\simus\Param_file.xlsx'.
File 'C:\Users\NK27504\Documents\MATLAB\internship\simus\Param_file.xlsx' not found.
I keep having this error messsage although my excel sheel is found in this directory.
can anyone help me what to do
  1 commentaire
Stephen23
Stephen23 le 26 Juin 2023
@Nadine: please show the output of this command:
dir C:\Users\NK27504\Documents\MATLAB\internship\simus\*.*

Connectez-vous pour commenter.

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by