Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X
Hello there.
I am currnelty explorign the use of the mathcad API for generating a matrix of data from another application.
I am using VS2010 Professional and VB.NET. I am using the mathCAD API for doing this. I am using MathCAD V14.0.2.5
It appears that there is a limit on the size of the matrix one can create through using the setelement mehtod of the MatrixValue class.
My original matrix was of size 6800 rows x 21 cols. When i try and view this matrix or assign this matrix to a new variable MathCAD shows no vlaue for this matrix. Teh matrix is composed of strings - i ahve to use strings as the data is of a variant/object type i.e. strings/doubles/integers. It seemed to crash when I got ro row 480. For rows less than this i was able to view the matrix in mathcad and assign the matrix to a another user defined valriable as well as extract elements for given indices.
As a test I simply ran a loop for a matrix of 480 rows x 18 columns and assigned an integer value of 1 for every elment. In this instance I was able to get mathcad to read the matrix; hwn trying to display it gave the following:
POINTSMatrix:=BigMatrix{480x19}
If I increase the range to 800x19 I cannot get any value for the matrix. If I use 600x19 it still works
Is there a limit on teh maximum size of a matrix that can be assigned using the SetElement method? IS there a better more efficient way fo assigning data to a very large matrix in mathcad using its API? I want to use the Automation API and not manually do this using table.s
My code for this is given below, I am using ealry bound code, hence Option Explicit = true, and using Ctype statements:
Dim mcdObject As New Object
Dim mcdPointsMatrix As Mathcad.MatrixValue
Dim mcdMatrixNew As Mathcad.MatrixValue
mcdMatrixNew = CType(Interaction.CreateObject("Mathcad.MatrixValue"), Mathcad.MatrixValue)
Try
mcdPointsMatrix = CType(mcdWorkSheet.GetValue("Points"), Mathcad.MatrixValue)
bMatrixExists = True
Catch ex As Exception
bMatrixExists = False
end Try
For i As Integer = 600 - 1 To 0 Step -1
For j As Integer = 18 To 0 Step -1
If bMatrixExists = False Then
mcdMatrixNew.SetElement(i, j, 1)
Else
mcdPointsMatrix.SetElement(i, j, 1)
End If
Next
Next
If bMatrixExists = False Then
mcdWorkSheet.SetValue("Points", mcdMatrixNew)
Else
mcdWorkSheet.SetValue("Points", mcdPointsMatrix)
End If
Make sure you create the last element first. Otherwise Mathcad keeps duplicating the old matrix as it adds the extra row/column when the next index is used.
I vaguely remember an absolute maximum number of elements that is related to your available contiguous memory size which may be an issue. Not sure if it still stands.
Most of my work has been simple API stuff from VBscript or Matlab.
Philip
Hi Phillip
thanks for the repsonse.
yes, good point but in the code above, i did initialize the array by first starting with the highest element and workign backwards.
What is the largest matrix you have created using the MathCAD API with VBScript/Matlab? could you share some code? Im testing my matrix and MathCAD keeps hanging unle sthe arrya is small. I tried an 8000x30 matrix called TEST which to me is small by mathcad stabards. The sheet keeps hanging when I assign X:=TEST.
If i explcitely define the same array in the MathCAD sheet using range assignments i.e
i:= 0..8000
j:=0..30
TESTi,j:="hdhskjhadskjhsakjhsa"
The matriux is cimputed super quick in time in the mathCAD sheet.
It seems that the API is not working properly.
Im using .NET, VS2010 and Win 7 x64
Thanks Steve
I have no idea what you mean by contagious smemory, but I supect what I want to do using the API is affected by this and it is currently impossible to do.
The documentaiton on the API is very poor compared to others like CATIA/Excel etc.
It seems crazy, but I cannot assign a matrix larger than 400x30 with a string value of "TEST" assigned ot each element. This is crazy. It is easliy possible to assign arrays far larger than this within the mathcad environment (in process) using explict range assignments on the sheet. Ridiculous. What use is the API if you can only work with such tiny matrices?
i.e. i:=0..400 j:=0..30 TESTi,j:="TEST"
Can anyone confirm what the maxium array size they were able to create within mathCAD using the API?
Is this poor performance due to incompatibilty of the Mathcad COM model with .NET when running out of process? I understand that CATIA has similar issues in that running out of process on a 64 bit OS is actually slower than runing in process using VBA scripts.
Currently, I simply cannot use MathCAD for out of process .NET situations and will have to consider another option such as Matlab/Mathematica/Maple
Id be interested if someone else could test the above. Its quite possible Im doing something really silly!!!!!
thanks
It appears to be a bug. A long standing one too. And it has nothing to do with dotnet.
The attached worksheet has a scripted component in to that can be use to create a matrix via the API. Attempts to create a large matrix fail in versions 11, 13 and 15. The fact that it fails in version 11 is significant, because they rewrote a large chunk of the compute engine for version 12. In version 11 the largest matrix seems to be larger, but not much, and when it does create it it is MUCH faster. Above a certain size though (not very big - 1000x30 will fail) version 11 simply exits with no messages and no warning! The behavior in versions 13 and 15 is similar. If you try to create a matrix that is too large Mathcad will either hang or crash.
The reason I say it's a bug, rather than an intentional limitation, is both because there is no reason why such a limitation should have been imposed, and because of the completely ungraceful way Mathcad handles it. If it were intentional then I would expect an error message, or, worst case, that it would simply do nothing. Not that Mathcad would crash!
Hi Richard.
Thank yo for looking at this and confirming it is abug with MathCAD.
This is very annoying and has thrown a spanner in the works.
Do you think it possibel for PTC to correct this and release and amended MathCAD API to address this severe limitation? Do you know who to contact? Is MathCAD prime also limited by this or is a new API used?
Thanks
Steve
Do you think it possibel for PTC to correct this and release and amended MathCAD API to address this severe limitation?
Maybe. It's evidently been there for a long time. I don't know if that means it's very difficult to fix, or they just didn't know about it or it was too low priority to get fixed.
Do you know who to contact?
If you have current maintenance you can log a formal report via the PTC website. If you are a single user you have no contact channel.
Is MathCAD prime also limited by this or is a new API used?
Prime has a new API, but it has not been released to the public for version 1.0 so that will not do you any good.
Man, this is a real pain.
The best solution I have is to stream the contents of the matrix values I want to create to an intermediate text comma delimted file. I will then call the file "Variable_file.txt" and then reassign this string as a variable to the WRITEFILE method. Problem with this is that it requires the user to set up the WRITEFILE construct beforehand so it is not a fully live and dynmaic solution which is the reason why I wanted to use the API in the first place!!!
Example:
'create txt file dynamically from data from external application, and pass name of inetrmediate text file to M_filename as string value using MathCAD API
Mfilename:="M_file,txt"
M:= READFILE(Mfilename,"delimited)
I tested this on a matrix of 6000x30 strings and it worked in less than 10seconds.
Is there any way I can dynamically create the READFILE(Mfilename,"delimited) construct and assing it to a variable?
Thank you so much for looking in to my problem and confirming what I suspected,
cheers
Problem with this is that it requires the user to set up the WRITEFILE construct beforehand so it is not a fully live and dynmaic solution which is the reason why I wanted to use the API in the first place!!!
But surely the user would have had to modify the worksheet for you to use SetValue? At least to the extent that if they have an assignment for the matrix, M, they must disable execution of that assignment. Although that is much less work than setting up the WRITEFILE!
Is there any way I can dynamically create the READFILE(Mfilename,"delimited) construct and assing it to a variable?
Maybe. You cannot create (or destroy) regions via the automation interface. You can change the XML of a math region though (but not any other type of region). So if the worksheet has an assignment for M you could locate that region and replace the XML with the READFILE statement.
Of course, this opens up another possibility. You could perhaps create the matrix directly by modifying the XML. This must be possible in principle, but I'm nor sure how easy it would be in practice.
I have attached a worksheet that might give you some useful code as a starting point for this approach. It has scripted components that read or modifiy the XML of a region. The worksheet was written by Tom Gutman, not me.
SetValue can create the matrix of strings in one operation. So create the matrix in VB, for example VBMatrix, and then call SetValue, for example
SetValue "MathcadMatrix", VBMatrix
will create MathcadMatrix as the variable
Hi Richard,
thanks for response. I tried your suggesiton but as expected, it made no difference.
Dim strTest(8000, 30) As String
For i As Integer = 8000 To 0 Step -1
For j As Integer = 30 To 0 Step -1
strTest(i, j) =
"hadkjhsadkhsakjhdkjsahdkjahdkjahskkdhakhskdkhakdksahk"
Next
Next
If bMatrixExists = False Then
Try
mcdWorkSheet.SetValue(
"TEST", strTest)
Catch ex As Exception
Dim btets As Boolean = False
End Try
Else
Try
mcdWorkSheet.SetValue(
"TEST", strTest)
Catch ex As Exception
Dim btets As Boolean = False
End Try
End If
What is the maximum matrix you have assigned using the MathCAD API and .NET?
thanks
Steve
three years later, I still have the same issue, pathetic
I would be amazed if they had fixed this. They are putting minimal effort into Mathcad 15 now, and a bug that affects so few people will never make iti high enough up the list. All their effort is going into Prime. At the current rate of progress maybe in another three years you will be able to do what you want in Prime. I wouldn't bet any money on that though
I have made a program that will import a matrix of data from a text file, and then send that file to MathCad via VBA, as I have alot of different files and want to be able to compare items dynamically and quickly.
So far, my program seems to do just fine when I have a matrix size of 111 x 105, but fails at 111x110. I have NO idea why this is, and can't seem to figure it out. It is very frustrating that this issue is occuring. I supposed I'll have to manually put that data in.
The thing is, I have a 'check' file in which i can put in the matrix as normal from data import wizard and it works JUST FINE! There is something wrong with the import API using the set value for very large matrixes. The solution may be to augment the matrixs if they are over a certain size?
Can't you just send the name of the text file to Mathcad, and let Mathcad read the file?
That might be the work around. Currently the text file is filled with other information that isn't specifically the data I need to pass to mathcad. I could export the data to a text file if the matrix is large, and then pass in that file.
Is there a specific API command to make that happen? I'm unsure if saying MC_Sheet.SetValue "Input_Data", 'FileName' would work. Input_Data is the matrix data I have, and MC_Sheet is an object, specifically the Mathcad.Worksheet object. I am only setting two values. One is the Diameter midpoint of the channels (just a singular vector) and have had no problems with it, while the other is the large matrix that has an entry for each diameter for each sample taken.
There's nothing specific. Just pass the path and filename to a variable as a string, and then use READPRN to read the data.
Richard,
Thank you very much. I altered the code to dump the matrix and vector to a text file (with overwrite true), and then passed those namepaths to my MC functions as strings for my input table. Everything seems to work just fine now, even for the larger matrixes. I do appreciate your insight into helping me solve this issue.
I was slightly worried about calculation time with the file writing, but the largest time part seems to be just the output variables I have from mathcad and setting those as output objects into my excel output table of the document.