Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Is there an easy way to tell if a variable or function has been defined.

Status
Not open for further replies.

PNachtwey

Electrical
Oct 9, 2004
772
0
0
US
I am a long time Mathcad and C programmer. In C one includes header files. Since many header files are often included, it is possible that a variable or function has been defined before so there is a test to see if the variable has been defined before, so it doesn't get redefined.

In Mathcad I often use the ability to include other mathcad files. What I need is a way to test if a variable has been defined before so it is not redefined
In short, Mathcad has no
#ifdef
#endif
feature like C or C++ have that I can see.
I have been using Mathcad since Mathcad 3 or about 30 years.
I am shocked that I haven't noticed this before.
I have about 1000+ Mathcad files and was starting to "clean them up" when I came across this problem. So many have Bode plots with common code or Pole Zero plots that are the same with only the variables being different.





Peter Nachtwey
Delta Computer Systems
IFPS Hall of Fame Member
 
Replies continue below

Recommended for you

In Mathcad are variables global by default?

Insofar as most variables are defined on the main worksheet; functions and programs have locally scoped variables, but can use global variables.

Global variables in sheets that are referenced become global variables of the referencing sheet, hence the need for a way to check if the variable defined on a referenced sheet.

TTFN (ta ta for now)
I can do absolutely anything. I'm an expert! faq731-376 forum1529 Entire Forum list
 
In SMath, I can use a variable name and instead of using := to describe the function, I can use =. If the value has not been defined, the program automatically assigns :=, I don't know if MathCAD does that.

-----*****-----
So strange to see the singularity approaching while the entire planet is rapidly turning into a hellscape. -John Coates

-Dik
 
Dik, the issue, I think, is that operation only works in real-time, i.e., you pick a spot on the sheet and type that in, but that doesn't work if you have an existing sheet and then add a referenced sheet to the start of the existing sheet. And variable already defined in the referenced sheet will simply get redefined in place, and any calls to functions created in the referenced sheet using that variable would use the new definition instead of the original definition.



TTFN (ta ta for now)
I can do absolutely anything. I'm an expert! faq731-376 forum1529 Entire Forum list
 
In a collpased area above each calculation group I zero out all variables being used in the section below using := if the section is intended to be local only. If I am going to use the calculation as a block or reference or insert I might include a collapsed area below the calculation to zero out local variables and/or show calculated results. If a section needs to carry or show values defined above then I include a collapsed area above and/or below with each of those using =

In essence I'm using programming technique I was taught in college - be sure to initialize before and clean up after blocks of code.

During the checking process I can open each collapsed area and see if the values make sense.
 
:(
The problem is that I must always manually define or undefine variables in the included/inner/child work sheet. I think the only solution is to NEVER define the variables in the included worksheet but have a test outer worksheet that defines varuables for testing the inner worksheet.
This way the inner worksheet never defines variables that override the outer worksheet's variables.
I just thought I would post my kludge solution. Frustrating.
For all its flaws. I still like using Mathcad for making YouTube videos. I have the whole worksheet tested and blow it up to 150% to 200% for the video. This way I don't waste time crating things on video like the professors do. This wastes time. The Mathcad worksheet is used as an outline too.




Peter Nachtwey
Delta Computer Systems
IFPS Hall of Fame Member
 
IRS... if an existing sheet, then by using the '=' operator will show a value and you can then move this value upwards until it 'blanks out'... and that is the location of where the value was defined.

-----*****-----
So strange to see the singularity approaching while the entire planet is rapidly turning into a hellscape. -John Coates

-Dik
 
Status
Not open for further replies.
Back
Top