cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

communication between VBA and Prime7 SetMatrixValue

JF_9255051
4-Participant

communication between VBA and Prime7 SetMatrixValue

Hello, 

 

I can´t figure out by myself the syntax to pass a matrix from excel via SetMatrixValue to Mathcad Prime 7.

I was able to write a single variable with "Call WS.SetRealValue("test", 6, "")" with no Problem but I can´t get it working with a matrix.

 

The code I use:

 

Dim MCPath As String
Dim path As String

 

path = Ordnerpfad(ActiveWorkbook.Path)

MCPath = path & "\Rechenkern\.......

 

Set MC = CreateObject("MathcadPrime.Application") 'CreateObject("Ptc_MathcadPrime_Automation.Application")
MC.Visible = True
MC.Activate

Set WS = MC.Open(MCPath)

Call WS.SetRealValue("test", 6, "")

Call WS.SetMatrixValue("test2", ?????, "")

 

The variable "test" and matrix "test2" already exist as inputparameters in Mathcad, I simply want to change the value of the matrix like I did with "test". I hope somebody can help me with my problem. 

ACCEPTED SOLUTION

Accepted Solutions

Hi

Try This

The origin of indices into the matrix starts at 0.  The API matrices are column major storage.

 

Set matrix = WS.CreateMatrix(3, 3)

x = 1
For ir = 0 To 2
For jc = 0 To 2
Call matrix.SetMatrixElement( jc, ir, ((i-1)*3+j)*x)
x = (x + 1)
Next jc
Next ir

Call WS.SetMatrixValue("test2", matrix, "")

View solution in original post

6 REPLIES 6

Hi,

Try this and see if it works.

The help for SetMatrixValue expects a matrix as the second parameter to the function so you need to create one in your code.

Listed below is a very simple coding as an example to create a matrix that should be able to pass.

 

SetMatrixValue(aliasArg as string, valueArg as Matrix, unitsArg as string)—Sets the matrix value and units to input with specified alias.
aliasArg is the alias of the input.
valueArg is the matrix value to be set to input.
unitsArg is the units of be set to input.
 
 
   Dim matrix() As Integer
   Dim x, i, j, k As Integer
're-dim the size of the array
   ReDim matrix(1 To 3, 1 To 3) As Integer
   x = 1
   For i = 1 To 3
      For j = 1 To 3
         matrix(i, j) = x
         x = (x + 1)
      Next j
   Next i
 

thank you very much for your help.

 

I am still receiving a message that the types are not compatible to each other. "Call  WS.SetMatrixValue("test2", matrix(), "")" 

The same message with "Call  WS.SetMatrixValue("test2", matrix, "")" 

Is there a mistake in my code? 

 

Dim matrix() As Integer
Dim x, i, j, k As Integer
're-dim the size of the array
ReDim matrix(1 To 3, 1 To 3) As Integer
x = 1


For i = 1 To 3
      For j = 1 To 3
              matrix(i, j) = x
              x = (x + 1)
      Next j
Next i


Call  WS.SetMatrixValue("test2", matrix(), "")

Hi,

Try Double as the matrix type?  

 

Dim matrix() As Double
Dim x, i, j, k As Integer
're-dim the size of the array
ReDim matrix(1 To 3, 1 To 3) As Double

 

Call  WS.SetMatrixValue("test2", matrix, "") 

Hi,

OK I searched the web and the MathCad forum and got the following example.

You need in your coding to use the API function CreateMatrix, to establish the size then API function SetMatrixValue to establish the contents before using SetMatrixValue

https://www.ptc.com/en/support/article/CS301830?

 

The contents of that follows.

 

CreateMatrix(rowsArg as integer, colsArg as integer)—Creates a matrix of given size filled by NaNs.
rowsArg is the number of rows.
colsArg is the number of columns.
Returns:
The created matrix or Null if an error occurs.

 

SetMatrixElement(RowIndex as integer, ColumnIndex as integer, Value as double)—Sets the matrix element located at RowIndex, ColumnIndex to Value.
where
RowIndex is the row index
ColumnIndex is the column index
Value is the matrix element value by specified row and column indices.

 

What follows is a different language but you can translate easily.


var appCreate = new Ptc.MathcadPrime.Automation.ApplicationCreatorClass();

Ptc.MathcadPrime.Automation.ApplicationCreator app;
app = (Ptc.MathcadPrime.Automation.ApplicationCreator)appCreate;

Ptc.MathcadPrime.Automation.IMathcadPrimeWorksheet3 ws;
ws = (Ptc.MathcadPrime.Automation.IMathcadPrimeWorksheet3) app.Open(WorksheetFileName);
int numRows = // whatever;
int numCols = // whatever;

Ptc.MathcadPrime.Automation.IMathcadPrimeMatrix matrixValue = (Ptc.MathcadPrime.Automation.IMathcadPrimeMatrix) ws.CreateMatrix(numRows, numCols);

for (int j = 1; j <= numRows; j++)
{
for (int k = 1; k <= numCols; k++)
{
double valueArg = // In my case I fetch valueArg from some other system here
matrixValue.SetMatrixElement(j - 1, i - 1, valueArg);
}
}
string aliasArg = // whatever
string unitsArg = // whatever;
ws.SetMatrixValue(aliasArg, matrixValue, unitsArg);

 

Hi

Try This

The origin of indices into the matrix starts at 0.  The API matrices are column major storage.

 

Set matrix = WS.CreateMatrix(3, 3)

x = 1
For ir = 0 To 2
For jc = 0 To 2
Call matrix.SetMatrixElement( jc, ir, ((i-1)*3+j)*x)
x = (x + 1)
Next jc
Next ir

Call WS.SetMatrixValue("test2", matrix, "")

Thank you very much for your help!

I was very busy until now and could not find time to continue coding until now.

I still can not imagine why this was not included in the code example from ptc for VBA!

 

 

Dim ir, jc, z, a As Integer
Dim matrix As matrix


Set matrix = WS.CreateMatrix(3, 3)
x = 1
For ir = 0 To 2
For jc = 0 To 2
Call matrix.SetMatrixElement(jc, ir, ((a - 1) * 3 + z) * x)
x = (x + 1)
Next jc
Next ir

Call WS.SetMatrixValue("test2", matrix, "")

 

this is the code that worked for me.

Announcements

Top Tags