Skip to main content
Werner_E
25-Diamond I
February 6, 2014
Question

Re: PTC Mathcad Matrix to C++ Code

  • February 6, 2014
  • 10 replies
  • 4949 views

Documents created by PTC staff don't allow for comments - so this is an attempt to comment on a recent document posted.

The file recently posted by Brent Edmonds (http://communities.ptc.com/docs/DOC-4875) contains two routines, gen_cplusplus() and WRITEFILE().

The first one takes a matrix consisting of numbers (no error checking) and creates a vector of strings similar to what you would type if you define and initialize an array in C(++). And this routine has some flaws in my opinion.

The second routine is, as is also stated in the enclosed word doc, the real gem. Its courtesy by Magnus Eklund from PTC and writes a matrix with arbitrary elements to a file and if a matrix element is a string it is written without the double quotes. Its sure a valuable utility routine for the collection.

Nevertheless some comments to this second routine first:

  • Please change the name from WRITEFILE() to something else, as WRITEFILE() is the name of a built in routine. In the attached file I included it renamed to WRITEMATRIX().
  • In case the matrix element is a number, it is converted to a string with num2str(). Then the routine looks if there is a comma (,) in the resulting string and if one is found, it is replaced by a decimal point. This should never be necessary, as Mathcads num2str() never produces a decimal comma, whatever the decimal character in the Windows setting may be. I should know, as we use the decimal comma over here and I often had problems with scripted components for Mathcad because of this, as the used VBS routine would consider the decimal character in the Windows settings and gives a decimal comma which Mathcad sees as an error.
    Anybody knowing why this replacement is done inMagnus Eklund's user function? Am I missing something?

Concerning the gen_cplusplus() routine, its a fairly basic one and somewhat long with a lot of if's for its purpose

  • as an "official" utility routine it should be ORIGIN aware; as its written now, ORIGIN=0 is assumed
  • I think the output should also include the dimensioning of the array, which can easyly be done, and when we are at it, why not create a complete definition with "float data [..." in front as well. Its easier to edit than to add/complete
  • The routine makes use of a strange "feature" that allows the usage of an undefined variable in a program if its used in a loop. At best its bad programming style but I think I would call this "feature" a bug in Prime (haven't tried if it works in Mathcad, too) - see below.
    undefined.png
    In my opinion test1 should also throw the error because of the undefined variable!
    EDIT: Just tested it and this behaviour is already present in Mathcad 15, too!

Find attached as simpler and bit more complete replacement for gen_cplusplus(), I called it mat2CPP() (also without error checking).

But as the goal is just the creation of the file an all-in-one routine like the attached WriteCPP() should do the job pretty well.

writecpp.png

P.S.: @unknown: Does it really makes sense to make a single document with just one small, very special utility? Wouldn't it make more sense to create a collection of 20 or more of such small routines in one file. Looks to me like pretending the existence of lot of content and that shouldn't be necessary.

10 replies

1-Visitor
February 7, 2014

Werner,

I did not export the array declaration thinking this would make it easy to go to JAVA and not too big a stretch for FORTRAN. I need to use str2vec more, thanks for your example.

Regarding your comment "looks like pretending the exisitence of a lot of content". .. well we have come to expect these comments from you. We will continue to post utilities customers find useful and just expect to hear from you.

John

23-Emerald I
February 7, 2014

John T. Sheehan wrote:

. .. well we have come to expect these comments from you.

John

This attack is unprofessional and uncalled for.

1-Visitor
February 7, 2014

It was not meant as an attack Fred. Simply a statement of our readiness and our plans to continue to post

1-Visitor
February 8, 2014

Thanks for this routine! I wasn't aware we could use this combination of str2vec and WRITEBIN to get rid of the double quotes.

As an asisde, I also consider John's remark on a rather constructive posting as absolutely out of place, unnecessary and inapprehensible.

Werner_E
Werner_E25-Diamond IAuthor
25-Diamond I
February 8, 2014

Leopold Turek wrote:

Thanks for this routine! I wasn't aware we could use this combination of str2vec and WRITEBIN to get rid of the double quotes.

Not my baby - the idea of doing it that way is from the WRITEFILE() routine in the referenced file posted by Brent Edmonds. The routine was said to be courtesy of PTC staff member Magnus Eklund.