Why can't MATLAB access a file that is in its current directory?
505 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 18 Aug 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
on 14 Nov 2019
There's another problem with those files. The ( and ) characters are not allowed in MATLAB script, function, or class names.
Answers (3)
Guillaume
on 18 Aug 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 Comment
Aeyzechiah Vasquez
on 2 Apr 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 Comment
Walter Roberson
on 2 Apr 2020
resources became forbidden as a directory a small number of releases ago; it was in the release notes, but easy to overlook.
Nadine
on 26 Jun 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
See Also
Categories
Find more on File Operations 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!