How to run function within a MATLAB-script (m-File) from Windows batch-file with arguments

5 views (last 30 days)
I have a Matlab-script (myscript.m) containing functions (e.g. myfunction) I want to call from a Windows batch-file (*.bat) with additional arguments, somewhat like this:
SET "myscript_folder=C:\Users\me\Documents\scriptFolder"
matlab -nosplash -noFigureWindows -wait -r "'%myscript_folder%\myscript.m', myfunction, 'U:\complexpath\text.txt', 'textargument', 300, 300)"
I have not yet understood how to call matlab with arguments from a batch file, especially when I am calling a .m-file in a different folder.
Where other answers have failed me:
  • Some answers to similar questions require the MATLAB script to be adjusted, for example to allow using predefined variables instead of arguments. Since I am using it in a research context, I don't want to alter the actual script, so other people can work with the same script.
  • For scripts, defining variables beforehand seems to be recommended. In my case however, I need to call a specific function within a .m-file.
  • Some answers suggest to switch folders (cd ...) and then call the function from there. In the context of my batch file however, I don't want to change current folders, since I need to loop over a number of local files, calling the matlab-script for each of them.
Is there a decent way to accomplish what I need?
  1 Comment
Mario Malic
Mario Malic on 18 Nov 2020
Hello Christian,
This is a really hard question to understand. You mention functions, scripts, batch, -r, text input, and lots of stuff that is contradictory. Best is to clarify things a bit.
Here's a link to read about non-interactive and interactive session of MATLAB. Link
matlab -batch "cd C:\Users; disp('test'); disp(pwd)"
% ^option [^ imagine you're writing this in MATLAB^]
General suggestion is that you don't change current directory, but rather work with full paths to files/folders/anything.
Your script calls a function, if that function is a file, you can just call it directly.

Sign in to comment.

Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!