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

Calling mathcad scripts VS programming toolbar

Pekko
1-Newbie

Calling mathcad scripts VS programming toolbar

Hello all!

Using the "programming" toolbar in Mathcad can be very effective, but it is not very feasible for long and complex programs. Is it possible/feasible to use scriptable components for implementing complex functionality, and is it possible to call your own worksheet functions and mathcad functions from within a script? (for example Genvals, Genvecs, Minimize etc...). Or is it so that scripts are not meant for calculation? I hope I was being clear enough.

Thank you!

3 REPLIES 3
MikeArmstrong
5-Regular Member
(To:Pekko)

Would you be able to provide an example. There are many complex functions written using the programming format. They can be hidden in a collapsed area if you do not wish to display them.

Mike

I once wrote a mathcad function that had multiple nested ifs in it. It looked like this:

if this

then that

otherwise

if this

then that

otherwise

if this

then that

...

x<-M*K

for i e 0..rows(K)

do something

...

...

The function was propably one page long. Complex functions like this start to get hard to understand and commenting them starts to get inconvenient. Chopping the function into smaller pieces isn't possible, because mathcad doesn't support pointers. Also mathcad's support for formatting your code isn't very good. That's why I would like to use an actual programming language (like basic) to do these kinds of things.

What I'm asking is, that if I define a simple function in my worksheet, like

square(x) := x*x

is it possible to call this function from within a script? That way I could use the programming toolbar for mathematical calculations and the scripting for complex logic. Or should I use a completely different program for those cases requiring complex logic?

Thank you for your time!

Ndjeol
1-Newbie
(To:Pekko)

Hello Pekko!

Yes, you can call that function from within a script simply by calling it as a refefence within any current worsheet. What you need to do is to:

1)- Save that function under any filename. My advice to you in this regard is to save it under a name that starts with the name of that function (without any brackets or parenthesis), just like M-files are saved in Matlab. You can add a very short comment to that name. For example, you could save your function as: "square - calculates the square of the argument". Of course without the quotations marks. Also, no other variable definition, except that function, should be written in the worksheet host of the function, otherwise it will also be called as a reference in your current worsheet and could bring confusions if it happens that you are using the same name to define another variable.

2)- In your current worsheet in Mathcad, click the on location where you would like the reference to appear then click Insert -> Reference -> Browse. Go to the directory where you saved your function (square - calculates the square of the argument) and double-click it or simply select-open it. Then click OK in that dialog box to close it. A line showing the path to your function will appear in your current worksheet. You can move that reference at will within the current worksheet. Now, it is ready to use just like if it was defined in the current worksheet: square(x)=, x must be defined. Or used in a script within the current worksheet.

Here is another tip: if you have many functions that you would like to call as reference in another worksheet, you may want to simply insert all of them, one after another as a reference is single worksheet, save that multi-reference worksheet under any name ("my reference functions" for example), then insert "my reference functions" as a reference in your current worksheet. You will be able to call any of those functions in the current worksheet.

Think that you are set. Good luck.

Top Tags