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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Creating a "for loop" using references to another worksheet

aaula
1-Newbie

Creating a "for loop" using references to another worksheet

Hi everyone,

I would like to know if there's a way to use a reference within a for loop in Mathcad 15. The subject has already been discussed here but the proposed solution wasn't really working well and gets too complicated with a bigger worksheet. Nevertheless, the principle is there.

To schematize, i'm trying to create a worksheet like this :

for i from 1 to n

* define variables used in reference calculation

* call reference

* get results(i) from reference

next i

show results

I hope this is technically possible, so please let me know if you know an advanced version of the pre-cited method.


Regards,


Antoine

1 ACCEPTED SOLUTION

Accepted Solutions
StuartBruff
23-Emerald II
(To:aaula)

Antoine Aula wrote:

Hi everyone,

I would like to know if there's a way to use a reference within a for loop in Mathcad 15. The subject has already been discussed here but the proposed solution wasn't really working well and gets too complicated with a bigger worksheet. Nevertheless, the principle is there.

To schematize, i'm trying to create a worksheet like this :

for i from 1 to n

* define variables used in reference calculation

* call reference

* get results(i) from reference

next i

show results

I hope this is technically possible, so please let me know if you know an advanced version of the pre-cited method.

In short, "No".

As you've probably found out from the previous threads, there isn't a built-in way to reliably loop over a general "reference" worksheet. All of the workarounds involve playing around with components to gain access to the automation interface.

One possibility is to rewrite your referenced worksheet in terms of range variables and define the range variables (plus any other application-dependent variables) in the calling worksheet, as per the simple example below.

Referenced Worksheet:

Calling Worksheet:

But this is rather messy and doesn't make full use of the power of Mathcad.

Needless to say (but I'll say it anyway) there is a Very Long Standing Feature Request to have this kind of capability built-in to Mathcad.

Stuart

View solution in original post

16 REPLIES 16
StuartBruff
23-Emerald II
(To:aaula)

Antoine Aula wrote:

Hi everyone,

I would like to know if there's a way to use a reference within a for loop in Mathcad 15. The subject has already been discussed here but the proposed solution wasn't really working well and gets too complicated with a bigger worksheet. Nevertheless, the principle is there.

To schematize, i'm trying to create a worksheet like this :

for i from 1 to n

* define variables used in reference calculation

* call reference

* get results(i) from reference

next i

show results

I hope this is technically possible, so please let me know if you know an advanced version of the pre-cited method.

In short, "No".

As you've probably found out from the previous threads, there isn't a built-in way to reliably loop over a general "reference" worksheet. All of the workarounds involve playing around with components to gain access to the automation interface.

One possibility is to rewrite your referenced worksheet in terms of range variables and define the range variables (plus any other application-dependent variables) in the calling worksheet, as per the simple example below.

Referenced Worksheet:

Calling Worksheet:

But this is rather messy and doesn't make full use of the power of Mathcad.

Needless to say (but I'll say it anyway) there is a Very Long Standing Feature Request to have this kind of capability built-in to Mathcad.

Stuart

AndyWesterman
4-Participant
(To:aaula)

You don't show the type of calculation that you want to achieve in the reference, BUT as long as it can be written as a function (or a number of functions) that can be called ; it may be possible.

The attached (re-worked sheets) is a trivial example, but it could be extended to cover most of the functionality of Mathcad.

Even solve blocks can be written as functions in parameterised form and could be called this way.

A Westerman wrote:

You don't show the type of calculation that you want to achieve in the reference, BUT as long as it can be written as a function (or a number of functions) that can be called ; it may be possible.

Aye, there's the rub! For in that referenced worksheet what calculations might be must give us pause. There's the respect that makes calamity of using references.

Many user make good use of range variables and 'top-level' expressions that Mathcad allows to be properly documented by Text Regions. A program is much harder to document in a manner that looks satisfactory.

It's rather a nusiance to have to rewrite such worksheets to get round Mathcad's lack of ability to either loop over a set of regions or to treat a worksheet as a standard component object (eg, Excel) and exchange data with it. A rewrite involves time (money) to do the conversion, validate it, review it and perform all the other myriad tasks that a half-way decent engineering process demands. It would be *so* much easier to not have to do it.

Stuart

RichardJ
19-Tanzanite
(To:StuartBruff)

The good news is that I had members of the PTC development team agree with me that adding the capability for regions to have inputs and outputs, and for them to be able to act as functions, would be a very useful addition to Mathcad. I don't know if this ever actually made it onto a to-do list though, or how many decades we will have to wait to see it even if it did.

StuartBruff
23-Emerald II
(To:RichardJ)

Richard Jackson wrote:

 

The good news is that I had members of the PTC development team agree with me that adding the capability for regions to have inputs and outputs, and for them to be able to act as functions, would be a very useful addition to Mathcad. I don't know if this ever actually made it onto a to-do list though, or how many decades we will have to wait to see it even if it did.

Good Job! It's nice to know the idea's in place, even if it might take some time to get implemented.

 

Is there any chance of making this a multi-generational aspiration for the descendents of the development team?

 

When you next speak to them, it might be worthwhile sowing the seed of making each region (more obviously) an object, with the ability to get at its attributes and methods programmatically as well as via a suitable dialog ... I think I suggested this back when Pangaea was still forming ... I think this as far back as PTC DeLorean thread chaser will go, though: Re: Mathcad worksheet as a function, https://community.ptc.com/t5/PTC-Mathcad/MathCAD-Prime-1-0-is-a-step-back/m-p/120871#159782%23159782%23159782 and Re: Format a Solve Block.

 

Stuart

RichardJ
19-Tanzanite
(To:StuartBruff)

Is there any chance of making this a multi-generational aspiration for the descendents of the development team?

Sort of like Cathedral building was in Europe in the middle ages? "Son, my father started the immense project of adding inputs and outputs to regions, and I've dedicated my whole life to it. I doubt I will see it's completion in my lifetime, but you will follow after me and hopefully bring this masterwork to fruition. The experience this will bring to you will prove invaluable, as you will then embark on the even greater project of Multi-Dimensional Arrays. The magnitude and importance of the MDA project will be greater than anything in recorded history, and may take centuries to complete. You should be proud to be a founder of such a project though, because it will change the world. Just think of it. Three or more array subscripts!".

StuartBruff
23-Emerald II
(To:RichardJ)

Richard Jackson wrote:

Is there any chance of making this a multi-generational aspiration for the descendents of the development team?

Sort of like Cathedral building was in Europe in the middle ages?

That's exactly what I was thinking, or a multi-generational starship that builds up high tau factors - hmm, so 1 year of ship time could be millenia of "real" time and, of course, you get the same result in the presence of strongly curved space-time ... so maybe we're doing them a disservice and there's a black hole at the centre of PTC in which they're working round-the-clock and kicking out daily (ship-time) versions of Prime, little realizing that decades have passed in User Time?

"Son, my father started the immense project of adding inputs and outputs to regions, and I've dedicated my whole life to it. I doubt I will see it's completion in my lifetime, but you will follow after me and hopefully bring this masterwork to fruition. The experience this will bring to you will prove invaluable, as you will then embark on the even greater project of Multi-Dimensional Arrays. The magnitude and importance of the MDA project will be greater than anything in recorded history, and may take centuries to complete. You should be proud to be a founder of such a project though, because it will change the world. Just think of it. Three or more array subscripts!".

I'm just imaging the tau factor necessary to get MDAs in place before the Sun decides to expand its operations into Earth orbit ... there's a nice little question for Mathcad: How fast will such a spaceship be going when it achieves sufficient mass that it gains its own event horizon?


Actually, I think it might be more prudent if I were to adopt some diplomatic(*) skills as it appears that not only do we PTC Engineers who have taken on-board the idea of regions as objects with exposed properties and methods, but we may have an adept in the art of multi-dimensional array indexing, ie Mr Sheehan of this Parish (who also seems to share my predilection for mangling the Bard's words!). See the following article: <a href=" />

John Sheehan says:

Couldn’t agree more Stuart. In a former life I did OLAP and everything was in a cube. I also agree that nested matrices is a poor substitution. Many of our customers are still using RDBMs. I think you are implying 3D databases are growing in popularity ?

So maybe we should start writing to PTC management telling them what a top notch bloke Sheehan is and why they should implement his ideas in Prime 3.2?

Or perhaps Brutus might have the apposite message to pass ...

Under your pardon. You must note beside,

That we have tried the utmost of our friends,

Our legions are brim-full, our cause is ripe:

The enemy increaseth every day;

We, at the height, are ready to decline.

There is a tide in the affairs of men,

Which, taken at the flood, leads on to fortune;

Omitted, all the voyage of their life

Is bound in shallows and in miseries.

On such a full sea are we now afloat;

And we must take the current when it serves,

Or lose our ventures.

Stuart

(*) OTOH, maybe I ought to leave the diplomacy to those best versed in it. One of my squadron commanders wryly noted that in my version of the Oxford English Dictionary the word "diplomacy" was synomous with the word "gunboat", and "tact" meant "nailed to the wall".

MJG
18-Opal
18-Opal
(To:aaula)

Depending on what you want to do with the output, you could consider creating your for loop in an Excel macro that inputs values into your Mathcad sheet and places the results in the Excel worksheet.  Someone with more experience might even be able to embed the Excel file into a Mathcad sheet so that you can then use the results in Mathcad instead of Excel.

The take away from this thread seems to be, have your functions in individual worksheets and when you want to use them copy and paste the code to your worksheet you want to use them in. Very brutish. Wouldn't it be better if there was a user editable path variable containing all the absolute path directories where custom functions are stored and then you could call your functions from any worksheet? For instance,

rainflow_matrix:= rainflow (vector, bins)

Then MathCAD would search the directories specified by path and when it found the rainflow.m file it would run the subroutine and return the output to the variable rainflow_matrix.

That might save you having to copy and paste, but it would have it's own downsides. Mainly, your worksheet would no longer be very portable, because you would have to also move every worksheet that contains a custom function. This is the reason I stopped referencing one worksheet from another years ago.

You missed my subtle diatribe. If this were Matlab we wouldn't be having this conversation.

I didn't miss it. I've received Matlab scripts with missing m files. And I actually really dislike the way Matlab requires functions to be in their own m files. If this were Matlab the conversation would be complaints from me that you can't put function definitions in the same file as the main script.

I guess you prefer MathCAD's molasses like speed as well for parsing huge data files?

No. Each package has it's own strengths. Given the choice of putting functions in a separate file (or in Matlab's case many separate files, because if you put two functions in one file you can only reference the first one) and then having a non-working script because it gets separated from the necessary files, or doing some copy and paste to put everything in one file, IMO the latter is a much better option.

I would like the option of functions in a separate file if it were implemented with one feature that Prime does have that Mathcad 15 does not. In Prime you have the option of embedding a copy of a referenced file. If the worksheet can't find the referenced file it uses the embedded copy. So Prime actually has the best of both worlds (or at least is closer to it than either MC 15 or Matlab). You can define functions in the same worksheet if you choose. You can define them in separate worksheets if you choose, and reference those worksheets. If the main sheet gets separated from the referenced sheets it still works. I don't use Prime though, because of all it's other limitations.

StuartBruff
23-Emerald II
(To:RichardJ)

Richard Jackson wrote:

No. Each package has it's own strengths. Given the choice of putting functions in a separate file (or in Matlab's case many separate files, because if you put two functions in one file you can only reference the first one) and then having a non-working script because it gets separated from the necessary files, or doing some copy and paste to put everything in one file, IMO the latter is a much better option.

I would like the option of functions in a separate file if it were implemented with one feature that Prime does have that Mathcad 15 does not. In Prime you have the option of embedding a copy of a referenced file. If the worksheet can't find the referenced file it uses the embedded copy. So Prime actually has the best of both worlds (or at least is closer to it than either MC 15 or Matlab). You can define functions in the same worksheet if you choose. You can define them in separate worksheets if you choose, and reference those worksheets. If the main sheet gets separated from the referenced sheets it still works. I don't use Prime though, because of all it's other limitations.

It is definitely good to have the referenced worksheets embedded in a worksheet.   Perhaps, though, what's also required is something that Mathcad <=15 almost had (in the form of Extension packs), and that is a proper library configuration tool for adding worksheets (and user DLLs) to an appropriate "Library" folder, with the ability to add folders to the "Path".   I think it would have to use a library tool, rather than just dump worksheets in a folder, to eliminate the no-doubt lengthy search through hundreds of worksheets to find a particular function,

It would also aid the user choosing the right version of function f from the hundreds of versions in the library.

I have a vague recollection, from before the cut-off date for transferring threads from the Collaboratory to the Community, of suggesting a more object-oriented version of Mathcad, one aspect of which would be the ability to declare worksheets to be objects and then be able to pick a particular version of a function by referencing its worksheet (eg, something like wks:="Help Me.xmcd" and to use, say, function f(x,y) from this worksheet you'd write wks.f(2,3) or just f(5) if you wanted to use the single argument version in your worksheet itself).

Stuart

RichardJ
19-Tanzanite
(To:StuartBruff)

Perhaps, though, what's also required is something that Mathcad <=15 almost had (in the form of Extension packs), and that is a proper library configuration tool for adding worksheets (and user DLLs) to an appropriate "Library" folder, with the ability to add folders to the "Path".

This was definitely a request that Mathsoft was aware of, and had on their to-do list (although I think with no specific delivery schedule). If that were combined with the idea that the most recent version of the relevant functions should be embedded in the worksheet as a backup, that would be a real winner for me. An alternative would be what Zemax (optical ray tracing software that I use) eventually implemented. Zemax now allows the user the ability to save a file that is a complete package, with any files it depends on included, with sufficient path information that the recipient can simply open it with no dependency problems.

Top Tags