Is there a way (command line switch) to prevent execution of startup.m when starting a new Matlab instance?
40 views (last 30 days)
Show older comments
Is there a way (command line switch) to prevent execution of startup.m when starting a new Matlab instance? How about the same for finish.m?
0 Comments
Accepted Answer
Jan
on 21 Oct 2016
Edited: Jan
on 21 Oct 2016
You can write a small batch job, which creates an envorinment variable before calling Matlab:
@echo off
set NoMatlabStartupM=1
matlab
Now insert this on top of the startup.m function:
NoMatlabStartupM = getenv('NoMatlabStartupM');
if ~isempty(NoMatlabStartupM)
return;
end
No startup.m is still called, but not executed, when you start Matlab through this batch file.
5 Comments
Steven Lord
on 18 Nov 2016
Moved: Adam Danz
on 4 Apr 2023
That sounds like a reasonable enhancement request to file with Technical Support. You can do so using the Contact Us link in the upper-right corner of this page. Please include the use case so that Support can capture it for the developers' future reference in the enhancement database entry.
Steven Lord
on 4 Apr 2023
Skipping finish.m is possible if you exit MATLAB using quit force. This allows you to bypass a (potentially misbehaving) finish.m file. This is a big hammer, and I don't know that I'd use it as part of my normal workflow, but it is an option.
Skipping startup.m is not possible AFAIK but I believe you could start your "main" MATLAB session in a directory that is not on the MATLAB search path but contains your startup.m file and start your "spawned" MATLAB sessions in a different directory.
More Answers (3)
Image Analyst
on 23 Oct 2016
You could write a temporary file in your startup.m to some known location. If the file exists, just exit the startup, but if not, write the file. Have a finish.m file where you delete the file when MATLAB (any instance) exits.
folder = 'c:\users\public\Documents'; % Wherever...
fullFileName = fullfile(folder, 'MATLAB is running.txt');
if exist(fullFileName, 'file')
% MATLAB is already running. So just exit the startup.m
return;
end
% File is not there and MATLAB is not running.
fid = fopen(fullFileName, 'wt');
fprintf('MATLAB started at %s', datestr(now));
fclose(fid);
Then in your finish.m file, which runs when you exit MATLAB, delete that file.
folder = 'c:\users\public\Documents'; % Wherever...
fullFileName = fullfile(folder, 'MATLAB is running.txt');
if exist(fullFileName, 'file')
delete(fullFileName);
end
2 Comments
Jan
on 26 Oct 2016
When you start 2 instances of Matlab and want to run one and the other without startup(), using a file as signal creates a racing condition.
Image Analyst
on 26 Oct 2016
When I ran one after the other had already started, it was no problem. If you tried to launch both at about the same time, you'd need to check which instance was the one that actually ran the startup.m file.
Eric Condamine
on 3 Jul 2019
After more than two years, is there now a way to launch matlab without using our personal startup.m and if possible our personal finish.m, by using only a special argument like -noStartupFileExecution? In fact it seems to be not too complicated, because it is like we start using only the default startup.m and finish.m file ...
2 Comments
Image Analyst
on 3 Jul 2019
I think so. Did you look at Specify Startup Options in the help where you'll see things like:
Startup Options in Shortcut on Windows Systems
You can add selected startup options to the target path for your shortcut on the Windows platform for MATLAB.
To use startup options for the MATLAB shortcut icon, follow these steps:
- Right-click the shortcut icon for MATLAB and select Properties from the context menu. The Properties dialog box for MATLAB opens to the Shortcutpane.
- In the Target field, after the target path for "matlab.exe", add the startup option, and click OK.
This example runs the MATLAB results script or function after startup, where results.m is in the startup folder or on the MATLAB search path. The text in theTarget field is similar to the following:
"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -r "results"
You could also put in a questdlg() or menu() question into your existing startup.m to popup a question asking you which, if any script or lines of code you want to run that time.
Rik
on 3 Jul 2019
Technically that isn't the same as starting a session without running startup.m, but I can't think of an example of when the difference would matter. (unless running this would still run startup.m secretly, I'm on mobile so I can't check, but it would necessarily surprise me)
Note that the -r syntax has been available since at least ML6.5, and probably way before that.
Eric Condamine
on 5 Jul 2019
The problem is just to start matlab without using the personal startup.m (et finish.m) file of the user, and for all platforms (not only Windows). I am developing a python app where I need to do somethings in matlab and catch the result (without using intermediate file, etc. I try to be as simple as possible). It will be easier to use the standard output from matlab, but in this case there are the usual starting Matlab message (from matlab_path/VersionInfo.xml file) all stuffs that can generate a display, from the user startup.m file. So It will be nice if a special matlab option exist (like -noStartupFileExecution as proposed by Matthias above) to start matlab in the "just after install way" (i.e. the default startup.m, finish.m, default path etc ...). Because right now, I need to use a workaround like the one below to do something that would be very easy to do if this option existed!
I use the python subprocess library to deal with the call system, stdout, stderr, etc ... So this is multiplatforms (I guess, because I develop only on real OS, so Linux and MacOS ;-
As an example a way to ask the version of matlab knowing the matlab_path and the spm path. I use the stderr possibility of fprintf to overcome the statup.m, finish.m, etc that I discussed above ...
matlab_path -nodisplay -nodesktop -nosplash -singleCompThread -r "addpath(spm_path); [name, ~]=spm('Ver'); fprintf(2, '%s', name(4:end)); exit" > /dev/null
0 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!