外部.mファイルで定義される関数の呼び出し

117 views (last 30 days)
龍太 坂本
龍太 坂本 on 4 Oct 2022
Moved: Kojiro Saito on 11 Oct 2022
.mファイルで定義している関数を別ファイルで実行する方法について質問です。
①の関数処理中に②の関数を上手く呼び出すことができません。
どのようにすれば上手く呼び出すことができるでしょうか?
①Production Serverコンパイラ―でコンパイルした関数
②sampleFunc.m
●コード
①で②を呼び出すコードは
sampleFunction = @sampleFunc;
sampleOut = sampleFunction(x);
②の関数は
function sampleOut = sampleFunc(x)
sampleOut = x;
end
●実現したい処理フロー
MathWorks.MATLAB.ProductionServer.ClientでProduction Serverにある①を呼び出し(ここはOK)
①から②を呼び出し(ここがNG)
●試したこと
・①と同じ階層に②を配置
・①と同じ階層に②をProduction Serverコンパイラで作成したctfファイルを配置
・table2cell.mなどの標準関数と同じ階層に②を配置
⇒ いずれも『関数 'sampleFunction' (タイプ'****' の入力引数)が未定義です。』のエラー
 ****の部分は入力として渡した変数の型が入ります。(table,cell,stringなど、どのパターンでもエラー)

Accepted Answer

Kojiro Saito
Kojiro Saito on 5 Oct 2022
sampleFunc.mがProduction Serverコンパイラの「アーカイブの実行に必要な追加ファイル」に入っていれば、①から②を呼び出すのができるようになります。
①と②が同じ階層か、②がMATLABのpathコマンドで表示される検索パスのフォルダにあれば、コンパイル時に自動的に取り込まれます。
WindowsのコマンドプロンプトからcURLで呼び出し。
curl -v -H "Accept: application/json" -H "Content-type: application/json" http://localhost:9910/myfunc/myfunc -d "{\"nargout\":1, \"rhs\":[100], \"outputFormat\":{\"mode\": \"large\",\"nanInfFormat\": \"object\"}}"
実行結果
* Trying 127.0.0.1:9910...
* Connected to localhost (127.0.0.1) port 9910 (#0)
> POST /myfunc/myfunc HTTP/1.1
> Host: localhost:9910
> User-Agent: curl/7.83.1
> Accept: application/json
> Content-type: application/json
> Content-Length: 85
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 59
< Connection: Keep-Alive
<
{"lhs":[{"mwdata":[100],"mwsize":[1,1],"mwtype":"double"}]}* Connection #0 to host localhost left intact
「MathWorks.MATLAB.ProductionServer.ClientでProduction Serverにある①を呼び出し」でMPSクライアント側でどのようにMPS呼び出しの処理を書いていますでしょうか?
また、デバッグ用にProduction Serverコンパイラで「クライアントのテスト」ボタンをクリックして、「CORSを有効にする」にチェックを入れて、「起動」を押して、MATLAB上でMPSの疑似インスタンスを開始してください。
エディターで①のmファイル(ここではmyfunc.m)を開き、sampleFuncを呼び出す前(ここでは2行目)にブレークポイントを張り、MPSの関数をクライアントから呼び出してみてください。xにどのデータ型が渡っているのか確認できます。
  1 Comment
龍太 坂本
龍太 坂本 on 11 Oct 2022
Moved: Kojiro Saito on 11 Oct 2022
ご回答いただき、ありがとうございます。
ご教授いただいた対応で解決できました。
>>sampleFunc.mがProduction Serverコンパイラの「アーカイブの実行に必要な追加ファイル」に入っていれば、
>>①から②を呼び出すのができるようになります。
アーカイブの実行に必要な追加ファイルに追加されている状態でコンパイルしたところ呼び出すことができました。
>>「MathWorks.MATLAB.ProductionServer.ClientでProduction Serverにある①を呼び出し」で
>>MPSクライアント側でどのようにMPS呼び出しの処理を書いていますでしょうか?
下記のような処理で実行しております。
using MathWorks.MATLAB.ProductionServer.Client;
public interface myFunc_MATLAB
{
[MWStructureList(typeof(型名称))]
object[] myFunc(引数);
}
var host = GetHost();
var url = "http://" + host + ":" + MatlabPortNo + "/" + fileName; //fileNameが.ctfファイルの名称
using (var matlabClient = new MWHttpClient())
{
//MATLAB計算処理
var matlabProxy = matlabClient.CreateProxy<myFunc_MATLAB>(new Uri(url));
MATLABResult = matlabProxy.myFunc(引数);
}
ありがとうございました。

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!