Skip to main content
1-Visitor
January 13, 2014
Solved

Redefining an undefined variable

  • January 13, 2014
  • 2 replies
  • 8164 views

Hi,

I currently use a scripted function that redefines the earlier variable if it was undefined because of any reason. Please have a look at the attached file, and while opening the file, make sure you say "No" in the dialogue box that prompts you to disable the macros.

While this function works well for single line variables as is evident, but I can't use it in a variable that has more than one lines. Is there a way to solve this problem?

Thanks

Omkar

PS: I found the redefining function somewhere in the forums.

Best answer by Werner_E

I didn't look at your scripted file with the redefine() as I don't think you will need it nor is there a chance of getting it to work with local variables and/or single vector elements, i guess.

In your demo sheet I don't quite understand why you switch from spline to linear interpolation. splines tend to oscillate and usually won't give you that much more precison. So why not stick to linear interpolation?

Anyway, look if the attached worksheet does what you want.

I replaced the interpolation routines and those routines won't fail anymore. but even if they would, the way the function get_coeff() is written it wouldn't matter.

2 replies

25-Diamond I
January 13, 2014

The scripted component you found just works for worksheet variables and not for variables local to a program.

If you are just trying to trap division by zero errors or the like you may consider using the "on error" construct.

13.01.png

12-Amethyst
January 13, 2014

The function "Redefine" is better for checking that a variable has been defined.

If it hasn't then it provides the means to detect that & set up a default condition, rather than a work sheet showing red errors everywhere & this can be useful.

For the application that you have I think that the Mathcad built-in function "on error" will serve better:

Capture.PNG

NaN is then the default value which means NotANumber to Mathcad.

Regards

Andy

ojoshi-21-VisitorAuthor
1-Visitor
January 13, 2014

Thanks Werner/Andy, I thought of using the "on error" function but it doesn't work in a situation where the variable is not defined. For instance, if you delete the vector that is assigned to G_v, leaving it undefined, you will see that H_v will be undefined as well. This is what I want to mitigate actually, to redefine the variables local to the program.

Any thoughts?

Thanks

25-Diamond I
January 13, 2014

1) the variable H_v is not undefined because V[2 is undefined but because the execution of the program defining H_v failed (division by zero). If if ReDefine would work for local variables it would not be executed but the program is left immedeately after the error occurs. If you don't want that, you have to use "on error".

2) At the moment I can't think of a situation where a redefinition of a non-existing local variable could be necessary. Local variables are created by local assignments. So if you use "on error" for every assignment which could cause an error, you are on the safe side. If you are just sloppy and forget to assign a local variable or forget about the RHS in a local assignment and neverthelss try to use it afterwards, its better to get an error, fix it by making the assignment rather than programming something like "if I forgot to assign that variable - and I am too lazy to scroll upward to look if i did - I do a ReDefine() just as a precaution".

A ReDefine() can be useful in (I think rather rare) occasions where you set up a sheet to be used by someone else and want to provide default values for some variables even if the user deleted the defaults you had set at the top or typed something like A:= with a missing right hand side.