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

480 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2017-8-18
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 2019-11-14
There's another problem with those files. The ( and ) characters are not allowed in MATLAB script, function, or class names.

请先登录,再进行评论。

回答(3 个)

Guillaume
Guillaume 2017-8-18
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.

Aeyzechiah Vasquez
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 个评论
Walter Roberson
Walter Roberson 2020-4-2
resources became forbidden as a directory a small number of releases ago; it was in the release notes, but easy to overlook.

请先登录,再进行评论。


Nadine
Nadine 2023-6-26
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

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by