How to run MATLAB files in batch mode (or as a batch process). Guide request.

122 views (last 30 days)
Alfonso
Alfonso on 19 Jun 2012
Answered: MHS on 18 May 2018
Hello, my problem can likely be resolved with a simple guide. Unfortunately I simply haven't been able to find one. I've only used MATLAB in interactive mode and have no experience with running anything in batch mode or as a batch proccess. So that said, could someone point me to a guide (or two, reading more never hurts) that could walk me through the steps I need to take?

Answers (4)

Walter Roberson
Walter Roberson on 20 Jun 2012
matlab -r "statement"
The statement should be the bit of MATLAB code you want to execute.
I recommend
matlab -noFigureWindows -r "try; run('C:\Path\To\YourScriptName.m'); catch; end; quit"
To be safe, YourScriptName.m should have instructions to cd() to the directory it is to work in.
run() cannot be used with functions, only with scripts. For functions,
matlab -noFigureWindows -r "try; cd('C:\Path\To\'); YourFunctionName(); catch; end; quit"
The above commands could be put into a .bat file, and could be invoked from any directory (because the mini-script changes directory.)
  1 Comment
Alfonso
Alfonso on 20 Jun 2012
Walter, I think you are spot on. I learned a bit more about batch files last night. I've included the recommended line in a batch file I wrote in notepad. It's running the script and the script is running the functions like normal. Now to check with my supervisor if this was the desired result.

Sign in to comment.


per isakson
per isakson on 19 Jun 2012
The name of the guide is the online documentation. It includes many examples and demos. Your question puzzles me
  1. what do you understand with "batch"?
  2. do you know "function" and "script" in Matlab?
Search for "Programming Fundamentals" and "Working with Functions in Files" in the online documentation.
See: Demos | Getting Started | Writing a MATLAB Program (5 min, 45 sec)
If you want to invoke Matlab from a "dos batch file" see:
Start MATLAB program (Windows platforms)
Syntax
matlab helpOption
matlab -automation
....
  2 Comments
Alfonso
Alfonso on 19 Jun 2012
If its in the documentation sorry to waste people time. I look around but didn't see the answers I was looking for. Perhaps my search wording was poor or I simply didn't recognize the solution in front of my face.
1) I don't have a clear understanding of batch. I am an intern writing a script that reads an *.xls, calls a few functions, and writes another *.xls. I have only used MATLAB in its interactive mode in the past. My supervisor wants me to get this script running in "batch mode". As far as i understand, that is to get my script in a state/filetype where i don't need to open MATLAB interactively to run it, but it will call the functions and libraries it need from MATLAB. I suppose its some type of complied version of an m-file?
2) I feel I have a good understanding of those. I wrote the script and functions that i want to get running in "batch mode". Admitedly they aren't bullet proofed yet, but so long as you supply the data correctly they work fine in interactive mode.
The end goal in to run this script and associated functions in an Isight loop. I think walter may be along the right idea of what I am trying to do.
I'll check out your suggestions and get back to you.

Sign in to comment.



MHS
MHS on 18 May 2018
Hi Everyone ! I have several .m files which take a lot of time to execute, I intend to execute sequentially those .m files while saving the workspace variables for each m-file. Do I need to write a batch file to do this while I am away and I can run the batch file which calls the m-files sequentially. Can anyone guide me how to do that for simple m-files . Will appreciate any help in this regard Maqsood

Community Treasure Hunt

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

Start Hunting!