Skip to main content
11-Garnet
December 16, 2014
Question

Best practice: list of variables in mathcad v.11 and above

  • December 16, 2014
  • 2 replies
  • 10424 views

Most of the time I use MC v.11, but I did not find (in my rapid checking) anything even in v. 15. Probably there is something in Prime, but it is not for me, yet. So here the inquiry.

In other similar Math apps, there is a window or a command to list the variables used so far in the worksheet.

How do MC users deal with it?

So far I tried to keep up with the list of variables in a separate Excel file, or separate MC worksheet.

But I also tought to use a vector in the same MC worksheet with an initial column vector of definitions and the command "stack" to update it, like this:

ListV := ("v = velocity" "s =space")

(and when a new variable is defined)

ListV := stack (ListV, "a =acceleration")

So anytime I type ListV I have the list of variables and a short definition of them (would be nice to have them in alphabetic order). Does anyone use a similar method or a better one? How does one deal with a worksheet with dozen if not hundred of variable definitions?

Thanks for any feedback.

2 replies

23-Emerald V
December 16, 2014

In M11 to M15 it is possible to wrote a component that will iterate over a worksheet and, via the xml properties, locate and list all of the defined variables.

Stuart

11-Garnet
December 16, 2014

Thanks Stuart,

but it is a bit cryptid for my not-so-high-expertise. Do you have a working example for instance to expand a bit how it works? Can it function with an open worksheet?

In fact, often I need the list when I want to define a variable. In several occasions I am pretty sure that I already did it. However the time spent searching where is it in the worksheet, is higher than the time spent re-writing it.

23-Emerald V
December 16, 2014

anthony Queen wrote:

Thanks Stuart,

but it is a bit cryptid for my not-so-high-expertise. Do you have a working example for instance to expand a bit how it works? Can it function with an open worksheet?

Not to hand, I'm afraid. I think a search of the collab might turn up an example, though.

In fact, often I need the list when I want to define a variable. In several occasions I am pretty sure that I already did it. However the time spent searching where is it in the worksheet, is higher than the time spent re-writing it.

Go to Tools/Preferences in the main menu bar. In the General Tabl of the Preferences dialog, ensure you have the "Context-sensitive equals signs" tick box checked. Then, whenever you type, say, x=, if it's not defined, then x:= will appear rather than x=. With the box checked Mathcad, will assume you want to create a new variable.

Alternatively, got to the Warnings tab on the Preferences dialog and check the warnings on redefinitions of User Defined functions, variables and/or vectors and matrices, as required.

The first method is generally less confusing to look at.

Stuart

23-Emerald IV
December 16, 2014

"(would be nice to have them in alphabetic order)". Use:

ListV := sort(stack (ListV, "a =acceleration"))

To separate the variable names from their definition (and allowing them to be sorted individually) you could use a matrix (2 columns, n rows):

ListV := ( "v" "velocity"

"s "space" )

ListV := csort(stack (ListV, ( "a" "acceleration" ) ), ORIGIN)

or

ListV := csort(stack (ListV, ( "a" "acceleration" ) ), ORIGIN+1)

You can also add a column with the actual value of each variable:

ListV := ( "v" v "velocity"

"s" s "space" )

And of cours you can write the list of variables to a text file with:

WRITEPRN(".\Variables.txt") := stack( ( "Name" "Value" "Definition" ), csort(ListV,ORIGIN))

Success!

Luc

11-Garnet
December 16, 2014

Thanks Luc, nice touches.

Yet, I am a bit puzzled: Am I the only one (or one of the few) that misses the feature "list of used variables", and that looks for workarounds?

23-Emerald IV
December 16, 2014

Hi Anthony,

No, you're not the only one. There have been more requests. I personally don't need it. If I want to know if a variable is already used, I first (try to) print its value (Variable=). Besides I have the redefinition flag enabled, so a variable gets flagged when redefined.

And I don't use MC's built-in unit definitions of feet, pounds and what have you. Instead I use my own set (SI, with all prefixes defined) that is specially tagged so no confusion between a variable and a unit.

Also I tend to use functions to perform my calculations, so that the section(s) with actual calculations that use design parameters are small: single glance overview.

Success!
Luc

P.S.

Ah, I see now that the 'printing' of the actual values of the variables has a catch. It will print the values that are current at the time of the last variable definition added to ListV; as opposed to the values that are current on print time...