Hannes H.
Hannes H.

Reputation: 121

Matlab Namespaces

I have the following problem: I have Namespaces in my folder structure e.g.

+Something

---Test1.m

---Test1.params

.params is just a file which contains JSON.

in Test1.m there is a function which loads the json via fileread. If there is no Namespace structure and everything is in root it's working fine. But Now I should be able to do:

fileread('Something.Test1.params');

but it cant find it.

Any suggestions?

Regards

Upvotes: 1

Views: 554

Answers (2)

Cris Luengo
Cris Luengo

Reputation: 60494

You are relying on MATLAB to find the file for you in its path. This is a bad idea, someone else could create a file with the same name and put it earlier in the MATLAB path (e.g. in their working MATLAB directory) and break your code.

The function mfilename returns the full path of the currently executing M-file. Use that path to find your data file:

p = fileparts(mfilename('fullpath'));
p = fullfile(p,'Test1.params');
data = fileread(p);

Upvotes: 0

Zizy Archer
Zizy Archer

Reputation: 1390

You should be using path to the file. Something.Test1 is indeed full name of the function/script, but full filename is "+Something\Test1.params".

Upvotes: 2

Related Questions