Pass parameter from batch file to .m Matlab program
8 views (last 30 days)
Show older comments
I have got a batch file which calls up a matlab program as follows :
1.) batch.bat
@echo off
echo Content-type: text/plain
echo.
echo %1
"C:\Program Files\MATLAB\R2024b\bin\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\xampp\htdocs\IHC\MATLAB_testing.m','%1');exit;"
2.) MATLAB_testing.m
fid = fopen('results.str','w');
fprintf(fid,'%s',param1);
fid = fclose(fid)
==> The batch file will be run with a parameter input, ie . C:> batch.bat 'testing', and I expect the paramter could pass to the MATLAB_testing.m with param1
final aim ==> param1 = "testing" when run with C:> batch.bat "testing".
How could I modify the coding for the purpose ?
0 Comments
Answers (2)
Jaimin
on 3 Oct 2024
Hi @Tik Ho HUI
In the provided code for the "batch.bat" file, the "param1" variable has not been created. To learn how to create it, please refer to the code below.
@echo off
echo Content-type: text/plain
echo.
echo %1
"C:\Program Files\MATLAB\R2024a\bin\matlab.exe" -nosplash -nodesktop -r "param1='%1'; run('<PATH To FILE>'); exit;"
Now, each time the "batch.bat" file is executed, it creates the "param1" variable in the workspace, allowing you to access it using the name "param1".
Kindly refer following code for better understanding.
% MATLAB_testing.m
if exist('param1', 'var') && ~(param1=="")
fid = fopen('results.str', 'w');
fprintf(fid, '%s', param1);
fclose(fid);
else
error('Parameter "param1" not found.');
end
For more information regarding “exist” function kindly refer following MathWorks Documentation:
I hope this will be helpful.
0 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!