VBA中执行调用matlab程序(二)

上一篇的疑问,得到解决,可以这样改写:   

Sub ts2()
fileToRun = "D:\OneDrive\matlab\matlab一键启动\" & "xy.m"
matlabpath = "D:\Program Files\MATLAB\R2018a\bin\matlab -nodisplay -nosplash -nodesktop -r  "
matlabCommand = matlabpath & " run('" & fileToRun & "');exit;"""
Shell (matlabCommand)
End Sub

  -nodisplay -nosplash -nodesktop 的意思是只打开命令窗口,不以通常的方式打开matlab。

省略的话,打开的界面和以matlab快捷方式打开完全一样。

Sub ts3()
fileToRun = "D:\OneDrive\matlab\matlab一键启动\" & "xy.m"
matlabpath = "D:\Program Files\MATLAB\R2018a\bin\matlab"
matlabCommand = matlabpath & " -r "" run('" & fileToRun & "');"""
Shell (matlabCommand)
End Sub
"D:\OneDrive\matlab\matlab一键启动\" 这一段可以被 ThisWorkbook.Path & "\"代替。
"xy.m" 可以用VBA中的Dir函数获取。
" -r " 中的-r前后必须带有空格。
"D:\Program Files\MATLAB\R2018a\bin\matlab" 启动路径可以用"cmd.exe /c matlab”代替 
Sub ts()
 MyPath = ThisWorkbook.Path & "\"
 File = Dir(MyPath & "*.m*")
 Shell "cmd.exe /c matlab -r "" run('" & MyPath & File & "');"""
End Sub

 



posted @ 2020-09-18 21:02  redufa  阅读(714)  评论(0)    收藏  举报