Get Matlab Startup directory

Hello,
I use a batch script to start Matlab and set the startup directory via the -sd switch.
Example:
:: Matlab startup directory
set STARTUP_DIRECTORY=%USERPROFILE%\Documents\Matlab\R2019a_x64\MyDirectory
:: Matlab Executable
set MATLAB_EXECUTABLE=%ProgramFiles%\MATLAB\R2019a\bin\matlab.exe
:: start Matlab with specified startup directory
call "%MATLAB_EXECUTABLE%" -sd "%STARTUP_DIRECTORY%"
I use this procedure because I work with multiple Matlab configurations (Java classpath, Java library path, Matlab path) in each of the startup directories.
I have a setup script which collects all required information and stores the 3 Matlab configuration files (javaclasspath.txt, javalibrarypath.txt, pathdef.m) in the startup directory. To get that directory in the current Matlab session i used the following (dirty but working) code:
char(java.lang.System.getProperty('user.dir'));
But since Matlab 2019a, this line always returns:
'C:\Users\<UserName>\Desktop'
I have also tried the userpath command but unfortunately, this always returns the same directory, no matter which directory is specified as startup directory:
'C:\Users\<UserName>\Documents\MATLAB'
Maybe someone has got a solution or workaround for my problem?
Thank you,
Markus

回答 (2 件)

Ben Cunningham
Ben Cunningham 2019 年 4 月 17 日
編集済み: Ben Cunningham 2019 年 4 月 17 日

0 投票

You can use :
>> pwd
to return the present working directory in MATLAB.
You define your procedure as :
  • I start Matlab with the modified startup directory (-sd switch)
  • Now I change to another directory where my software is located e.g. D:\MyProject\MySoftware
  • In this directory I execute a setup M script
Why not :
  • I start Matlab with the modified startup directory (-sd switch)
  • I save the startup directory to a variable
startDir = pwd
  • Now I change to another directory where my software is located e.g. D:\MyProject\MySoftware
  • In this directory I execute a setup M script
Your setup M script then uses the variable 'startDir' to copy the files as required.

3 件のコメント

Markus Aigner
Markus Aigner 2019 年 4 月 17 日
The software is not only used by me but can be used by other people who use Matlab, too.
In this case I don't have the possibility to execute the mentioned command.
The people get our software, create a startup directory for this software (to have a clean Matlab configuration and/or to not mess up their current configuration), switch to our software directory and execute the setup script.
When they re-start Matlab with the modified startup path, the Matlab environment is prepared to use our software.
Ben Cunningham
Ben Cunningham 2019 年 4 月 17 日
編集済み: Ben Cunningham 2019 年 4 月 17 日
It seems to me that if the user is creating this start up directory, then why not use
startDir = uigetdir
in your script and let the user navigate to it.
Additionally, they could create the directory at the same time in the UI. I know it's not exactly what you're after.
Markus Aigner
Markus Aigner 2019 年 4 月 23 日
Thank you for your quick response and some ideas for an alternative setup procedure.
I may think about your suggested solution or a similar one (e.g. giving the user the possibility to pass the "configuration directory" as optional argument for the setup script).
Maybe it would be a nice feature for future releases to get this user defined startup directory via the userpath command?

サインインしてコメントする。

Markus Aigner
Markus Aigner 2019 年 4 月 17 日
編集済み: Markus Aigner 2019 年 4 月 17 日

0 投票

Thank you but unfortunately I cannot use this solution.
My procedure is as follows:
  • I start Matlab with the modified startup directory (-sd switch)
  • Now I change to another directory where my software is located e.g. D:\MyProject\MySoftware
  • In this directory I execute a setup M script
This setup script generates the Matlab configuration files which shall be copied into the startup directory (for the next Matlab startup).
Because I have already changed the current working directory (in this case to D:\MyProject\MySoftware), I cannot use the pwd command.

1 件のコメント

Ben Cunningham
Ben Cunningham 2019 年 4 月 17 日
I've updated my answer - I think this does what you need?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2019a

質問済み:

2019 年 4 月 17 日

コメント済み:

2019 年 4 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by