Is there a way (command line switch) to prevent execution of startup.m when starting a new Matlab instance?

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?

 Accepted Answer

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

Is there a self-contained solution that does not require modifying files on the host?
What does "self-contained" mean here? I do not think that you will find an easier solution without modifying startup.m .
Thanks for your answers! What I meant with "self-contained" is that the suppression of startup.m-execution would only apply to individual Matlab instances as needed, without modifying anything on the system (which might break other users' stuff). My use case is that I want to spawn new Matlab processes from within my main Matlab session for some background processing, but I don't want those processes to run startup.m (e.g. to speed up startup or because they should have a different path environment anyway). It would be nice to have a command line option like
matlab -nodesktop -nosplash -noStartupFileExecution
For now, I run a check in the startup/finish files whether the current instance was started with the -nodesktop option (usejava('desktop')), which happens works for my use case and is simple, but not as general as your solutions.
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.
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.

Sign in to comment.

More Answers (4)

A slightly hacked way of avoiding startup.m and finish.m in your personal directory, is to set the starting directory -sd to a folder where you have an empty starup.m and finish.m. As the files in the current directory will take priority over others in your search path, MATLAB will execute those instead.
As part of CI/CD worklfow I'm working on, which I also want to be able to execute locally, I will create those two empty files in the working directory before launching MATLAB, thereby ensuring more identical execution.
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

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.
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.

Sign in to comment.

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

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:
  1. Right-click the shortcut icon for MATLAB and select Properties from the context menu. The Properties dialog box for MATLAB opens to the Shortcutpane.
  2. 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.
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.

Sign in to comment.

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

Categories

Tags

No tags entered yet.

Asked:

on 21 Oct 2016

Answered:

on 7 Apr 2026 at 12:05

Community Treasure Hunt

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

Start Hunting!