Why am I unable to run MATLAB files that contain spaces in the file name?

24 visualizzazioni (ultimi 30 giorni)

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 1 Feb 2024
Modificato: MathWorks Support Team il 1 Feb 2024
This is not a bug; rather, it is an intended functionality of MATLAB. There are no ways to call functions whose file names contain spaces, so the only workaround is to rename your files so that they do not contain spaces.
Note that you can access directories with spaces in their names as described in the following answers post:
However, the workaround listed in that solution will not work for function file names.
  3 Commenti
Walter Roberson
Walter Roberson il 18 Nov 2023
To access a directory that has a space in the name, pass it as a character vector or as a string scalar. For example,
dinfo = dir('C:\MATLAB 7.8\MyProject');
cd('C:\MATLAB 7.8\MyProject')
Or, if you are using "command" form https://www.mathworks.com/help/matlab/matlab_prog/command-vs-function-syntax.html then quote the filename,
cd 'C:\MATLAB 7.8\MyProject'
What will not work is using command form without quoting the filename:
%the below might fail
cd C:\MATLAB 7.8\MyProject
Historically, there were cases involving Simulink technology in which spaces in a directory name would cause failure. I think Simulink itself has been fixed for those.
Historically, there were cases involving third-party technologies in which spaces in a directory name would cause failure -- for example some of the development environments related to Arduino or to Xilinx can fail if there are spaces in the name. For those situations there are two possible solutions:
  1. Rename the directories or files to avoid spaces and special characters; Or
  2. [Windows] Use the "short file name" for the directories and files that have spaces or special characters https://superuser.com/questions/348079/how-can-i-find-the-short-path-of-a-windows-directory-file . You might need to enable 8.3 filename creation; see https://support.trustwave.com/kb/KnowledgebaseArticle12180.aspx
Walter Roberson
Walter Roberson il 18 Nov 2023
Note that fileparts has multiple separate outputs: folder, then base name, then file extension. If you ask for only the first output, then that will be the folder.
fileparts() only parses the input it is given, and never attempts to search for a file, and never assumes the file is in the current directory, so if fileparts() returns empty folder information, it is because it was not able to find folder information in what you passed to it.
Also, when executed on Linux or MacOS, fileparts will not treat : as a drive separator, and will treat \ as just a character with no special significance
[myfolder, mybasename, myext] = fileparts('D:\Project 7\plan9.m')
myfolder = 0×0 empty char array
mybasename = 'D:\Project 7\plan9'
myext = '.m'
The above output is correct for Linux and MacOS, both of which can store files literally named ['D' ':' '\' 'P' 'r' 'o' 'j' 'e' 'c' 't' ' ' '7' '\' 'p' 'l' 'a' 'n' '9' '.' 'm']
If the same command were executed on Windows, you would expect a different output.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by