# Pass a UQLink model to a .m file model

**URL:** <https://uqworld.org/t/pass-a-uqlink-model-to-a-m-file-model/1012>\
**Category:** Community Q&A and How To\
**Tags:** uqlink\
**Created:** [January 22, 2021, 4:21pm UTC](https://uqworld.org/t/pass-a-uqlink-model-to-a-m-file-model/1012 "2021-01-22T16:21:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gil](https://uqworld.org/letter_avatar_proxy/v4/letter/g/9fc29f/32.png) [@Gil](https://uqworld.org/u/Gil)\
**Post date:** [January 22, 2021, 4:21pm UTC](https://uqworld.org/t/pass-a-uqlink-model-to-a-m-file-model/1012/1 "2021-01-22T16:21:25Z")

</div>

Hello everyone,

I defined a computational model through a .m file function. The function takes the random input X and gives the output Y. In the .m file, I define a UQLink model (which links UQLab to a FEM software) and I use the result of the FEM analysis (the FEM result is extracted thanks to a parser and is a scalar) as input for another computation which is implemented in the .m file. So the output Y is dependent on my FEM result and from other computations performed in the .m file. The problem is, that a new UQLink model is created each time the .m file function is called (at the end of the analysis I have in the folder many .mat files and .zip files called Model1, Model2, …, ModelN). Is there a way, to define the UQLink model outside the .m function and pass it, so that I only have to keep in the .m file the line which starts the FEM analysis? Something like:

`function Y = myFunction(X,myUQLinkModel)`  
`...`  
`FEMresult = uq_evalModel(myUQLinkModel,X)`  
`Y = f(FEMresult)`  
`end`

Another problem I have, is that if I perform an AKMCS, even if I limit the max added samples, say MaxAdded = 30, it runs forever and doesn’t stop. But I don’t know if it is related with the problem of having a new UQLink model each time the function Y is called.

Thank you!

---

<div class="post-metadata">

**Author:** ![moustapha](https://uqworld.org/user_avatar/uqworld.org/moustapha/32/226_2.png) [@moustapha](https://uqworld.org/u/moustapha)\
**Post date:** [January 23, 2021, 1:32pm UTC](https://uqworld.org/t/pass-a-uqlink-model-to-a-m-file-model/1012/2 "2021-01-23T13:32:56Z")

</div>

Hi @Gil

You can do what you suggested by passing the UQLink model as parameter of your myFunction model. Basically it would go like:

```
Mopts.mFile = 'myFunction';
Mopts.Parameters = myUQLinkModel ;
myModel = uq_createModel(Mopts);

```

Then you can define your model exactly as you have suggested:

`function Y = myFunction(X,myUQLinkModel)`  
`...`  
`FEMresult = uq_evalModel(myUQLinkModel,X)`  
`Y = f(FEMresult)`  
`end`

The UQLink model is defined in the main configuration file of your analysis, prior to defining `myModel`.

I think with this change you shouldn’t anymore have multiple models defined.

Generally speaking, also remember that when you have to build a model repetitively in UQLab (which is not the case for your problem here), you can use the options ’`-private'` as in

`myModel = uq_createModel(Mopts,'-private');`

to avoid saving in memory intermediate models that were built throughout the analysis.

For UQLink, you can also use the `.Archiving` option to clear the files created during the FE analysis.

Regarding the AK-MCS issue, that is a bit weird and I can’t reproduce the error from my side. Could you please try again with the updates above and let me know if you still have the same issue?

Cheers,  
Moustapha

---

<div class="post-metadata">

**Author:** ![damarginal](https://uqworld.org/user_avatar/uqworld.org/damarginal/32/73_2.png) [@damarginal](https://uqworld.org/u/damarginal)\
**Post date:** [February 3, 2021, 12:50am UTC](https://uqworld.org/t/pass-a-uqlink-model-to-a-m-file-model/1012/3 "2021-02-03T00:50:11Z")

</div>

10 posts were split to a new topic: [AKMCS does not terminate after maximum added sample reached](https://uqworld.org/t/akmcs-does-not-terminate-after-maximum-added-sample-reached/1027)

---

<div class="post-metadata">

**Author:** ![damarginal](https://uqworld.org/user_avatar/uqworld.org/damarginal/32/73_2.png) [@damarginal](https://uqworld.org/u/damarginal)\
**Post date:** [February 3, 2021, 12:50am UTC](https://uqworld.org/t/pass-a-uqlink-model-to-a-m-file-model/1012/4 "2021-02-03T00:50:56Z")

</div>


