[英]Calling a software from Matlab
在運行軟件以及生成報告和輸出文件的所有方面,命令提示符都可正常運行。 要生成包含所需結果的輸出文件,我們必須運行使用參數文件的報告程序的可執行文件。 例如,如果我要在命令提示符下實現這些步驟,它將是這樣的:
“path\report.exe” –f Report.rwd –o Report.rwo
輸出文件是Report.rwo,此文件將包含導出的變量。
現在要在Matlab中實現此功能,下面是一個小腳本,簡要說明了我要實現的目標。 每次運行都會調用該軟件並提取數據。
for nr=1:NREAL
dlmwrite(‘file.INC’,file(:,nr),’delimiter’,’\n’); % Writes the data file for each run
system('"path\file.dat"'); % calls software
system('"path\Report.rwd" –o "path\Report.rwo"'); % calls report
[a,b]=textread(‘"path\Report.rwo".rwo’,’%f\t%f’); % Reads the data and store it in the variable b
end
所以我有兩個問題:
1)當我在Matlab中運行此腳本時,它不會生成輸出文件Report.rwo。 因此,由於缺少文件,當它到達包含“ textread”功能的行時會給出錯誤消息。
2)每當Matlab調用報告(.rwd文件)時,它提示我按Enter或鍵入“ q”退出。 如果假設有數百個文件要運行,那么對於每個文件,我都會被提示按Enter鍵繼續。 以下行引起提示:
system('"path\Report.rwd" –o "path\Report.rwo"'); % Calls report
較舊的編輯:我的問題有2個更新,如下所示:
更新1:看來我上面問題的第2部分已經由Jacob解決。 一次運行正常。 但是,只有當我能夠運行涉及運行數百個文件的整個程序時,才能確認最終結果。
更新2:我可以運行該軟件並使用命令提示符生成輸出文件,如下所示:
**“path\mx200810.exe” –f file.dat**
此命令讀取報告參數文件並生成輸出文件:
“ path \\ report.exe” –f Report.rwd –o Report.rwo
最新編輯:
1)我能夠運行軟件,避免出現提示按回車鍵並使用Matlab通過以下命令生成輸出文件的情況:
system('report.exe /f Report.rwd /o Report.rwo')
system('mx200810.exe -f file.dat')
但是,只有將所需的.exe和.dll文件復制到我的.dat文件所在的文件夾中后,我才能執行此操作。 因此,我通過所有這些文件所在的文件夾運行.m文件。
2)但是,在Matlab的命令窗口中仍然存在一個錯誤,指出:
"...STOP: Unable to open the following file as data file:
'file.dat'
Check path name for spaces, special character or a total length greater than 256 characters
Cannot find data file named 'file.dat'
Date and Time of End of Run: .....
ans = 0"
用" .. "
括起來的字符串在MATLAB中無效,因此我不知道您的system
功能如何運行。
將所有"
替換為'
,然后更新您的問題,並在引號內包括命令行參數(例如-f file.dat
),如下所示:
%# Calls software
system('"path\mx200810.exe" –f file.dat');
%# Calls report
system('"path\report.exe" –f Report.rwd –o Report.rwo');
這是解決第二個問題的廉價方法(輸入q
終止程序):
%# Calls software
system('"path\mx200810.exe" –f "path\file.dat" < "C:\inp.txt"');
%# Calls report
system('"path\report.exe" –f "path\Report.rwd" –o "path\Report.rwo" < "C:\inp.txt"');
C:\\inp.txt
),該文件包含字母q
和返回字符。 您可以通過打開記事本,鍵入q
,按返回鍵並將其保存為C:\\inp.txt
來創建此C:\\inp.txt
。 這將用作“輸入” report.exe
似乎需要的。 system
調用,以便將我們剛剛制作的文本文件中的輸入傳遞到其中。 我已經在上面包含了修改后的調用(滾動到最后以查看區別)。 使用兩個輸出來獲取系統運行狀態和文本結果(如果有)。
cmd_line = '“path\report.exe” –f Report.rwd –o Report.rwo';
[status, result] = system(cmd_line);
根據status
變量繼續執行腳本。 如果超過零則停止。
if (status)
error('Error running report.exe')
end
[a,b]=textread(...
如果您的參數是變量,則可以使用字符串連接或SPRINTF函數在MATLAB中生成命令行字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.