Can't run script in namespace directory

조회 수: 13 (최근 30일)
Ratchet
Ratchet 2024년 6월 12일
답변: Steven Lord 2024년 6월 12일
Hello,
why is it that one apparently can't run scripts that are placed in namespace directories, i.e. direcotries that have their name start with a + ?
The reason I have a script in a namespace is that it containes some examples of how to use the code in the namespace. (It also seems like it's not possible to add a namespace directory to the path.)
  댓글 수: 1
Image Analyst
Image Analyst 2024년 6월 12일
What is the "current folder" in MATLAB, and is that the folder where your script lives? If not, is the + folder on your path? Type
>> path
to find out. How did you try to add it to the path? Via the Set Path button or the addpath function?

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

답변 (1개)

Steven Lord
Steven Lord 2024년 6월 12일
You can run a script in a namespace. You need to use the name of the namespace as part of the command, just like you would when you call a function. Let's make a script in a namespace:
cd(tempdir)
mkdir +mynamespace
cd +mynamespace
fid = fopen('myscript2127936.m', 'wt');
fprintf(fid, "disp('hello')");
fclose(fid);
cd ..
Now run it.
mynamespace.myscript2127936
hello
You are correct, you cannot add a namespace folder to the path. If you try MATLAB issues a warning.
addpath(fullfile(pwd, '+mynamespace'))
Warning: Namespace directories not allowed in MATLAB path: /tmp/+mynamespace
But you could import the namespace then run the script without the namespace name. Normally I don't like calling import with a wildcard (it could break your code if someone else adds something to the namespace without your knowledge that will take precedence over something you call) but given that I created the namespace specifically for this example it's okay in this case.
import mynamespace.*
myscript2127936
hello

카테고리

Help CenterFile Exchange에서 Use Prebuilt MATLAB Interface to C++ Library에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by