Pass parameter from batch file to .m Matlab program
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Jaimin
2024-10-3
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!