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

501 ビュー (過去 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.
  1 件のコメント
Miguel Mattis
Miguel Mattis 2019 年 11 月 14 日
I've been having this exact problem, thank you so much

サインインしてコメントする。


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 Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by