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

조회 수: 5 (최근 30일)
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
Mario Malic
Mario Malic 2020년 11월 18일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by