Is it possible to programmatically check whether MATLAB has been started with the "-sd" option?

I have a program that needs to behave differently depending on whether MATLAB was started with the "-sd" option (sets initial working folder) or not. However, I cannot determine a way to programmatically check this.

4 Comments

Describe this start scenario in more detail -- what is starting the session and where is the option/command being set?
Suppose that the option were used, but the folder named was the same as the folder that would otherwise have been used. Do you need to detect that case?
Is the real concern whether the option was provided, or is the real concern whether the current directory is not the same as the default userpath(), or is the real concern whether some configuration needs to be redone (such as loading in data)?
Sorry for being inprecise. I want to find out if matlab was started with
#!/bin/bash
matlab -nosplash -sd $matlabWorkingPath
See Matlab start options e.g. for Win or Linux.
Depending on that, I want to go to the last working dir or not. So far, my startup.m overrides the -sd request.
startup.m
%% go to last working dir
if ispref('StartupDirectory','LastWorkingDirectory') % && isempty(matlabStartOption('-sd')
lwd = getpref('StartupDirectory','LastWorkingDirectory');
try
cd(lwd)
catch
disp('Sorry, but I could not go to your last working directory:')
disp(lwd)
cd('Z:\MATLAB')
end
end
clear lwd;
So, one solution would be to identify if Matlab was started with the -sd option.
TMW hasn't provided a link to the command line -- I see a couple alternatives...(I've not tried either, just possible approaches that come to mind at the moment).
  1. Use a batch file containing the startup command and have it set an environment variable that startup.m can check, or
  2. Write a mex command that returns the command line and use the -r option to dispatch it. I don't know that this will work or not...

Sign in to comment.

 Accepted Answer

PID = feature('getpid');
[status, out] = system(sprintf('ps -p %d -o args', PID))
status = 0
out =
'COMMAND /MATLAB/bin/glnxa64/mvm/MATLAB -r setupcomputeserver -softwareopengl -desktop -appendlogfile /var/mlsedu/log/matlab.log '
Parsing the char vector out is not trival: E.g. he name of the logfile might contain the substring ' -sd '. Will masking substrings included in double quotes avoid such troubles?
Alternatively you can avoid -sd but use a dedicated function instead:
matlab -r "cd('/your/folder')"

1 Comment

PID = feature('getpid');
[status, out] = system(sprintf('ps -p %d -o args', PID))
works excellent under Linux, but the windows equivalent
PID = feature('getpid');
[status, out] = system(sprintf('tasklist /FI "PID eq %d"', PID))
does not provide arguments.
matlab -r "cd('/your/folder')"
is an excellent Workaround.
Thanks!

Sign in to comment.

More Answers (0)

Products

Release

R2022a

Asked:

on 17 Sep 2022

Commented:

on 20 Sep 2022

Community Treasure Hunt

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

Start Hunting!