Skip to main content
1-Visitor
January 5, 2022
Solved

communication between VBA and Prime7 SetMatrixValue

  • January 5, 2022
  • 1 reply
  • 3334 views

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. 

Best answer by terryhendicott

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, "")

1 reply

21-Topaz II
January 5, 2022

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
 
1-Visitor
January 5, 2022

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(), "")

21-Topaz II
January 5, 2022

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, "")