[英]Pass and read custom argument to an octave script from powershell/batch
我想知道在一般情況下從 Powershell/batch 執行八度腳本時是否可以直接傳遞自定義參數。 讓我們舉個例子:
$octaveExePath = "C:\Octave\Octave-7.1.0\mingw64\bin\octave-cli.exe"
# Executing the script
& $octaveExePath test.m "some_custom_string"
有沒有辦法從test.m
腳本中接收自定義參數?
編輯 1
我已經完成了@Ander 的建議但沒有成功,我想在這種情況下 matlab 和 octave beaviuor 真的不同。 我的結果制作和example.m
文件
function []=example(arg1)
disp(arg1)
endfunction
然后從powershell調用它
& "C:\Octave\Octave-7.1.0\mingw64\bin\octave-cli-7.1.0.exe" example.m asd
返回錯誤
error: 'arg1' undefined near line 2, column 7
error: called from
example at line 2 column 2
編輯 2
更具體地說,我的目標是知道是否有一種方法可以將參數從 shell 直接傳遞到 octave 程序,而無需使用某些外部文件,例如在本文中所做的那樣
我已經闡述了一個簡單的解決方法,可以在 Windows 環境中工作時將變量傳遞給 octave 腳本。 問題是在 Windows 下不可能像 文檔中所說的那樣創建 Octave 可執行程序。
無論如何,這個解決方法在某種程度上接近@Tasos 的建議,我最深切的感謝。
假設我們有一個包含一行代碼的tentative.m
文件:
disp(external_variable)
沒有變量聲明。
在外面,我們只按順序執行幾行 powershell 代碼(確保 powershell 的位置與您創建腳本的位置相同):
$octaveExePath = "C:\Octave\Octave-7.1.0\mingw64\bin\octave-7.1.0.exe"
$externalVariable = "just a little try"
& $octaveExePath --eval "external_variable='$($externalVariable)'; tentative"
這個結果與輸出:
just a little try
因此,通過這種方式,我通過外部執行“繞過”了--eval
和FILE
命令行選項互斥的事實(據我所知),成功地將外部值“傳遞”到了 octave 腳本。 唯一要注意的是,在使用此技術時,非初始化變量(在腳本中)必須與外部評估的變量名稱相一致(名稱必須相同)。
我希望已經清楚並且這可以幫助其他人,請考慮在這種情況下投票。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.