Lets just say, for arguments sake, I have MWCellarray temp:
MWCellArray temp = new MWCellArray(10000,11);
And I now wish to call a Matlab function to run some simple calculations upon it:
MLApp.MLApp matlab = new MLApp.MLApp();
<THIS IS THE MISSING STEP>
matlab.Execute("open 'C:\Program Filez\mymatlabcode.m'");
matlab.Execute("[thisismyoutput] = mymatlabcode(temp)");
I now need to make sure I can pass temp into the matlab workspace first. Obviously my first thought was:
matlab.PutWorkspaceData("putworkspace", "base", temp);
But no: {"Not implemented (Exception from HRESULT: 0x80004001 (E_NOTIMPL))"}
Seemingly I am going to have to turn the MWCellArray into a string array to use that command....
//{
// int[] kDimensions = temp.Dimensions;
// string[,] stemp = new string[kDimensions[0], kDimensions[1]];
// for (int i = 0; i < kDimensions[0]; i++)
// {
// for (int j = 0; j < kDimensions[1]; j++)
// {
// stemp [i, j] = (temp[i + 1, j + 1]).ToString();
// }
// }
//}
This is very slow. And the real implementation is around 15 of the MWCellArrays and this conversion is taking 15-20 minutes.
So. How can I do this in a sensible fashion? I cannot see any other methods to enable the transfer. Neither matlab.PutCharArray nor matlab.PutFullMatrix seem to be applicable.
Is there a way to speed up the loop considerably? I am entirely new to c#
Alternatively I would have thought I ought to be able to save an MWCellArray as a .mat file on C drive and then load it? (loading is easy, saving has me stumped)
I would be interested in any insights you can offer. Thanks.
I think you may be confusing two types of interaction between MATLAB and .NET.
MLApp.MLApp
. The resulting object then has methods such as PutCharArray
, PutWorkspaceData
etc, which would take regular .NET System.Array
variables as input; Execute
to run a MATLAB command; and GetFullMatrix
etc which return regular .NET System.Array
variables as output.MWCellArray
, MWNumericArray
etc as inputs, and return them as outputs.You can't mix those two by, for example, passing a MWCellArray
to matlab.PutWorkspaceData
.
If your aim is to call a live copy of MATLAB, don't create MWCellArray
variables - just pass over strings and arrays with PutCharArray
and PutFullMatrix
, execute stuff, then get the results back with GetCharArray
and GetFullMatrix
. Here's an example from the MATLAB doc.
If your aim is to call a deployed .NET assembly, then instantiate that and call it (passing MWCellArray
etc) rather than MLApp.MLApp
. Here's an example from the Builder for .NET doc.
Save it as an ascii file, and load it likewise into matlab? the format of the ascii file seems pretty simple: load documentation. The problem now is how to save as ascii from C#, which seems a bit simpler than the original problem to me :)
The only real format requirement is an equal number of elements on each row. You're limited to 2d matrices this way of course :(
User contributions licensed under CC BY-SA 3.0