Matlab Batch Run Returns Sript-as-Function Error

I am attemptting to follow along on this blog post but am getting the following error below. The myscript matches exactly the one in the post. I am not really sure how the the post's matlab instance and mine are different but clearly something. Any help would be lovely.
Ultimately, I am trying to use the command below specifically because it starts an independant process and will close terminate automatically after the script runs. I need to kick off a bunch of different instances of this.
I read the documenation for 2020a, and it seems like it is doable
The errors are below:
>> "C:\Program Files\MATLAB\R2020a\bin\matlab.exe" -batch myscript
"C:\Program Files\MATLAB\R2020a\bin\matlab.exe" -batch myscript
Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
>> matlab -batch "myscript"
Attempt to execute SCRIPT matlab as a function:
C:\Program Files\MATLAB\R2020a\toolbox\matlab\general\matlab.m

4 Comments

Stephen23
Stephen23 on 12 Jul 2026 at 16:08
Edited: Stephen23 on 12 Jul 2026 at 16:09
The blog is running those commands from the OS terminal, not from within the MATLAB IDE.
Ergo, you should run them from the terminal too.
dpb
dpb about 3 hours ago
Edited: dpb 33 minutes ago
To just add to @Stephen23 comment -- there isn't such a thing as starting a script as a batch job if you're already in MATLAB; that only makes sense if actually at a command prompt.
What are you really trying to accomplish here with "I need to kick off a bunch of different instances of this." since a bunch of different "Hello World" processes isn't of much practical use. Are you perhaps wanting to run the same code over a bunch of files, maybe? What, specifically do you think you get by running multiple batch jobs instead of a loop at the IDE?
Or, maybe what you are really after is to spawn a separate instance from MATLAB is why were trying this inside? Context could perhaps help besides the answer to the specific question.
I was using the "Hello World" as a test script since the actual simulation I need to run takes over two hours to run and I need to run ~200. I wanted to directly do it through Matlab due to how the system I have access to where launching a few different instances doesn't bog down the computer. Doing it in a single instance of Matlab will take a month of running but if I can kick off 10 instances, then it comes down to ~3 days. I can't use the Parallel Computing Toolbox due to one of the simulation dependacies (built outside of matlab) prevents parallel runs.
"Or, maybe what you are really after is to spawn a separate instance from MATLAB is why were trying this inside?" - This is exactly what I was trying to do. I really need to stop asking questions after too many long days beating my head against this problem.
I've added a comment to the linked blog post mentioning that the "matlab <list of startup options>" commands need to be run from the OS prompt, not the MATLAB prompt, unless you're using system or the ! operator. Hopefully that will forestall similar confusion in the future.

Sign in to comment.

Answers (2)

Fangjun Jiang
Fangjun Jiang about 2 hours ago
Edited: Fangjun Jiang about 2 hours ago
"C:\Program Files\MATLAB\R2020a\bin\matlab.exe" -batch "myscript"
is run at the Windows Command Prompt, not inside MATLAB Command Window.
dpb
dpb about 18 hours ago
Edited: dpb about 1 hour ago
EDIT - Forgot the newline after each write to the file in the original -- dpb
Still guessing a little about the actual use case, but try something like
pgm='"C:\Program Files\MATLAB\R2020a\bin\matlab.exe" -batch '; % base command line
d=dir('InputFileNames*.m'); % a list of all the m-files to be run (~200)
N=numel(d); % how many there are total
nperbatch=10; % how many to put in a given queue
for i=floor(N/nperbatch) + ~(mod(N,nperbatch)==0) % for how many batches
fid=fopen('batch%02d.cmd',i,'wt'); % a batch file for the group
fprintf(fid,'@echo off\n'); % turn echoing off in the batch file
ix0=(i-1)*nperbatch; % first for the group
for j=1:nperbatch % for that many
ix=ix0+j; % the next file in order
if ix>N, continue, end % we're done, leave loop
[~,mfile]=fileparts(d(ix).name); % strip the trailing .m
cmd=[pgm fullfile(d(ix).folder,mname) '\n']; % create a specific case command
fprintf(fid,cmd); % and put it in the batch file
end
fclose(fid) % save that batch file...
end
The above will create a serices of .CMD batch files "batchNN.cmd" that will each have the command to start a MATLAB process running the given m-file, again assuming there is a series of uniquely named m-files to be run. That's about the only way I see if each case to be run is actually a script and not a function. If they were functions instead, the -batch parameter value string is passed directly to the MATLAB interpreter so whatever syntax would be required to run the function would go in place of the above m-file names.
When ready to start another group, just execute the resulting batch file from a CMD session. When that group finishes, you can then submit the next batch. Not completely automated; would need a way to monitor that the first batch are done and wait for that flag.

Categories

Products

Release

R2020a

Asked:

on 12 Jul 2026 at 14:49

Commented:

about 1 hour ago

Community Treasure Hunt

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

Start Hunting!