How can I specify the search path from within a livescript (within a matlab project)?

Hi,
I have decided to try to change my way of working so that I use Matlab projects with all of my .m files replaced by livescripts. The first problem I have come across seems to be the path of the livescript.
In the original .m file, I obtained the path where the .m file was stored using the following:
currentpath = mfilename('fullpath');
I then obtained parent directories using the "fileparts" function. In this way, I can give the paths of my data files to be imported, and matlab functions to be called, relative to the parent directory. I also use "cd" to change the current directory to one of the parent directories, making the search path for all the scripts I am using very easy.
After converting my file to a livescript (located within a Matlab project), this method no longer works. I instead get the following ...
K>> mfilename('fullpath')
ans =
'C:\Users\st\AppData\Local\Temp\Editor\LiveEditorEvaluationHelperEID1207843749'
which is a completely different path to where the livescript file is located ('C:\Users\st\Dropbox\...\Project\InputScriptsFilesAndData').
I then get an immediate error message that the first function that I call from my script cannot be found.
So, please can somebody help me to work with the search path when using livescript?
Many thanks,
Sarah

5 Comments

You could try dbstack, although that might change for each release (requiring different skip depths). The 'run section' functionality has similar issues. I really wish there was a robust way to fix this.
That was a good suggestion, but unfortunately dbstack gives the same problem:
currentpath(1)
ans =
struct with fields:
file: 'LiveEditorEvaluationHelperEID1902544956.mlx'
line: 15
name: 'LiveEditorEvaluationHelperEID1902544956'
It refers to the strange new filename and path (the original file path is 'C:\Users\st\Dropbox\...\Project\InputScriptsFilesAndData\HMR_MainRunFile.mlx').
Actually, since the doc for mfilename doesn't mention any exceptions for live scripts, this behavior could even be considered a bug. Have you looked in the bug tracker if it is already mentioned?
You could also consider using pwd, although that will of course only work if the current directory contains your live script.
Hi Rik,
I agree that it seems like a bug. I don't how to see the "bug tracker" though. Where can I see that?
"pwd" was a good idea, and does work within a livescript, but like you say, there is nothing to force the current folder to be the same as the file that the file is stored when you run the script.

Sign in to comment.

Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Release

R2019a

Asked:

on 23 May 2019

Commented:

on 24 May 2019

Community Treasure Hunt

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

Start Hunting!