C# - Keep in memory variables between calling 2 different matlab routines
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
owr
le 26 Juil 2011
1 vote
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 commentaires
owr
le 26 Juil 2011
Ok - the docs are not great on this so here is an example of how I do it. Lets say "matlabroutine1" creates a variable that "matlabroutine2" needs to use that you dont want to pass back and store in C# (I do this with MATLAB objects all the time). What you need is a common variable name that is passed into both methods from C#
In ML, your code can look something like this:
function matlabroutine1(varName)
% The variable name doesnt matter within the function
varValue = <your specific formula>
% Save it off using the input variable name
setmcruserdata(varName,varValue)
function matlabroutine2(varName)
% Get the saved variable using the input varName
varValue = getmcruserdata(varName);
% Do whatever you need with it...
alorenzom
le 27 Juil 2011
owr
le 27 Juil 2011
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!
Catégories
En savoir plus sur Deploy to C++ Applications Using mwArray API (C++03) dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!