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

조회 수: 408 (최근 30일)
Fahad
Fahad 2017년 8월 18일
댓글: Stephen23 2023년 6월 26일
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
Aeyzechiah Vasquez 2020년 4월 2일
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
  댓글 수: 1
Stephen23
Stephen23 2023년 6월 26일
@Nadine: please show the output of this command:
dir C:\Users\NK27504\Documents\MATLAB\internship\simus\*.*

댓글을 달려면 로그인하십시오.

카테고리

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