codeaviator
codeaviator

Reputation: 2565

Run hidden .m file

I was wondering if it's possible to run hidden .m files in MATLAB. Take the following MWE:

My working directory contains two files: main.m and .foo.m which is hidden (in Linux, hidden files are preceded by a .)

dir
 |
 +-- main.m
 +-- .foo.m (hidden)

The file .foo.m contains:

disp('bar');

And main.m contains a call to .foo.m via the run command

run('.foo.m');

When I run main.m, MATLAB throws an error:

>> main
Error using run (line 61)
.foo.m not found.

Error in main (line 1)
run('.foo.m');

However if I run ls -a and dir, they both display the hidden file inside the directory:

>> ls -a
.  ..  .foo.m  main.m

>> dir

.       ..      .foo.m  main.m  

So it seems that MATLAB cannot find hidden files by default (at least in Linux).

Is there a way to enable running hidden scripts or functions in MATLAB?


EDIT:

Just realized that putting a . at the beginning of the file violates MATLAB's file naming rules:

Source: Specify File Names

"File names must start with a letter, and can contain letters, digits, or underscores."

Upvotes: 0

Views: 388

Answers (1)

Aero Engy
Aero Engy

Reputation: 3608

As you have already figured out you can not run m-files that don't conform to the naming convention. Meaning the file can not start with a .

However, if your intention is just to limit access to the "hidden" file you could make it a private functions. See Documentation

Private functions are useful when you want to limit the scope of a function. You designate a function as private by storing it in a subfolder with the name private. Then, the function is available only to functions in the folder immediately above the private subfolder, or to scripts called by the functions that reside in the parent folder.

Upvotes: 1

Related Questions