フィルターのクリア

C# - Keep in memory variables between calling 2 different matlab routines

3 ビュー (過去 30 日間)
alorenzom
alorenzom 2011 年 7 月 26 日
Is it possible to keep in memory variables used in a 1st matlab routine called by C#, when I call a 2nd matlab routine?
For example if I have in my C# application
matlabroutine1(); matlabroutine2();
and matlabroutine1 allocates a variable value1, when I call matlabroutine2 value1 is available?

採用された回答

Walter Roberson
Walter Roberson 2011 年 7 月 26 日
This may be a situation in which using global variables would be appropriate.

その他の回答 (1 件)

owr
owr 2011 年 7 月 26 日
What you are looking for is "setmcruserdata" and "getmcruserdata". Im assuming you are using the Builder NE. If so, also make sure that "matlabroutine1" and "matlabroutine2" are both methods in the same deployed C# class. These methods will share a common workspace in the MCR that will enable them to share data in the way you want.
  3 件のコメント
alorenzom
alorenzom 2011 年 7 月 27 日
Dear
I tried the following project:
---------------------------------------------
I builded this
---------------------------------------------
// Class1.cs
namespace Interfaccia
{
public interface Class1
{
void dammiA(out int Num,string varNm);
void Aggiungi(string varNm);
}
}
---------------------------------------------
---------------------------------------------
then I builded this
---------------------------------------------
% Aggiungi.m
function Aggiungi(varNm)
value = 10;
setmcruserdata(varNm, value);
end
% dammiA.m
function b = dammiA(varNm)
b = getmcruserdata(varNm);
end
---------------------------------------------
---------------------------------------------
then I builded this
---------------------------------------------
namespace Solutore
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Class1 Calcolo = new Calcolo.Class1Class1();
int b;
string varNm ;
varNm = "varNm";
Calcolo.Aggiungi(varNm);
Calcolo.dammiA(out b, varNm);
}
}
}
---------------------------------------------
---------------------------------------------
and after executing button1_Click I got at "Calcolo.dammiA(out b, varNm);"
ArgumentException non è stata gestita
Cannot convert MWArray to requested type
owr
owr 2011 年 7 月 27 日
You have the correct idea with the .m functions you wrote. I think the issue is in how you are calling it in C#:
Calcolo.dammiA(out b, varNm);
You declared "b" to be an int, all outputs from deployed MATLAB functions need to be some sort of "MWArray". It looks like you are trying to use an interface wrapper with "Class1". I would leave this out until you are comfortable working with MWArray's. I would expect that you will have to do something more like this:
MWArray[] mlout = Calcolo.dammiA(1, varNm); // "1" means you are only expecting one output.
int b = (int)(MWNumericArray)mlout[0] // Since you know the first(only) output is an integer
The documentation for Builder NE has many examples of how to work with MWArrays.
Good luck!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDeploy to .NET Applications Using MWArray API についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by