Quantcast
Channel: PTC Community : Discussion List - PTC Mathcad
Viewing all articles
Browse latest Browse all 2936

Re: PTC Mathcad Matrix to C++ Code

$
0
0

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 routines 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 becuase of this, as the used VBS routine would consider the decimal character in the Windows settings.
    Anybody knowing why this replacement is done? 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


Viewing all articles
Browse latest Browse all 2936

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>