cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

Modifying Matrix Elements From Within Scripted Object

epainter
1-Visitor

Modifying Matrix Elements From Within Scripted Object

I am trying to make changes to matrices with a VBScript Scripted Object. I can get the matrix with worksheet.getValue("Matrix"), but changes I make to that with Matrix.SetElement do not persist outside the script. It seems like the matrix returned by worksheet.getValue is a new object rather than a reference to the one in the worksheet, is that correct? If so is there any way to modify the original matrix from within the VBScript?

Thanks,

Evan

ACCEPTED SOLUTION

Accepted Solutions
StuartBruff
23-Emerald III
(To:epainter)

Evan Painter wrote:

I am trying to make changes to matrices with a VBScript Scripted Object. I can get the matrix with worksheet.getValue("Matrix"), but changes I make to that with Matrix.SetElement do not persist outside the script. It seems like the matrix returned by worksheet.getValue is a new object rather than a reference to the one in the worksheet, is that correct? If so is there any way to modify the original matrix from within the VBScript?

Thanks,

Evan

Yes, but not if the variable (scalar, string or matrix) is _defined_ within the worksheet, otherwise the Mathcad setting overrides the scripted change.

Stuart

View solution in original post

8 REPLIES 8
StuartBruff
23-Emerald III
(To:epainter)

Evan Painter wrote:

I am trying to make changes to matrices with a VBScript Scripted Object. I can get the matrix with worksheet.getValue("Matrix"), but changes I make to that with Matrix.SetElement do not persist outside the script. It seems like the matrix returned by worksheet.getValue is a new object rather than a reference to the one in the worksheet, is that correct? If so is there any way to modify the original matrix from within the VBScript?

Thanks,

Evan

Yes, but not if the variable (scalar, string or matrix) is _defined_ within the worksheet, otherwise the Mathcad setting overrides the scripted change.

Stuart

Okay, Thanks. So that basically means I can only manipulate matrices that are defined via a scripted object originally? I think I can work with that. Thanks.

RichardJ
19-Tanzanite
(To:epainter)

No, you misunderstand how SetValue works. SetValue binds the variable prior to any definition in the worksheet, regardless of the location of the scripted component. That means any worksheet definition simply overwrites it. So you can't read matrix M, modify an element, and write matrix M back to the worksheet. The definition of M in the worksheet overwrites the changes. You could write back a matrix M2, and as long as there's no definition of M2 in the worksheet it can be evaluated and used in calculations. You can get some quite weird behavior doing things like this though. Why do you want to modify it from within a scripted component?

Yeah that's what I meant. Since I can't manipulate a matrix defined in the worksheet (and have it persist) I can instead create the matrix in a scripted object, then use it for calculations in the worksheet, and modify it through more scripted objects.

I am trying this at the request of a coworker as an experiment to see if it will be more efficient than the way modifying a matrix/table is handled within a worksheet. We have some sheets that have a number of tables, which are modified by functions/formula within the sheet. Within sheet each time a table is modified they have to create a copy of it apparently, which is causing sheets to lag.

I expect this really won't be faster, in the end, but I figured I would give it a try and see what happens. If nothing else it is a chance for me to familiarize my self with MathCad and VBScript a bit.

If anyone has other suggestions for speeding up the operation of modifying elements in a table, I am open to suggestions.

Thanks

RichardJ
19-Tanzanite
(To:epainter)

I'm not sure what you mean by "each time a table is modified they have to create a copy of it". If I create a matrix, M, and then modify it in any way, for example M:=M*2, or M[0,0:=3, that does indeed create a new copy of M internally. In that sense you cannot modify the value of any variable in Mathcad, you can only create another copy of it with a new value (or values) assigned (so there really are no true variables in Mathcad, only named constants). If M is very large this can lead to memory issues. Is this the problem they are having?

I guarantee you that modifying the matrix from within a script will not be faster than modifying it from within the worksheet. There's significant overhead in calling a script within a component. In some limited circumstances it might actually possible to do what you are trying to do, but not using SetValue. You would need to modify the XML of a math region. So if you had

You could modify the values in the initial assignment of M from with a scripted component. However, you can't modify the values in the new copy of M created by the operation M:=M*2.

Yeah, basically that is the issue (from what I understand at least).

RichardJ
19-Tanzanite
(To:epainter)

OK. If I do this:

I have 4 copies of M. This is necessary, because you could evaluate any of them, so Mathcad has to store all the versions so that it could display them.

However, if I do this:

I only have two copies of M. The memory needed for the versions of M inside the program is freed as soon as the program exits. Mathcad can do this because you can't display those versions of M anyway, so it doesn't need to store them. So, if it's possible put as many of the modifications as you can inside programs.

Thanks, I will keep that in mind and see if we can consolidate as many changes as possible.

Announcements

Top Tags