# UQLink: pass paremeters to parser

**URL:** <https://uqworld.org/t/uqlink-pass-paremeters-to-parser/799>\
**Category:** Community Q&A and How To\
**Tags:** uqlink, rbdo\
**Created:** [August 11, 2020, 6:58am UTC](https://uqworld.org/t/uqlink-pass-paremeters-to-parser/799 "2020-08-11T06:58:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colette\_Jost](https://uqworld.org/user_avatar/uqworld.org/colette_jost/32/590_2.png) [@Colette\_Jost](https://uqworld.org/u/Colette_Jost)\
**Post date:** [August 11, 2020, 6:58am UTC](https://uqworld.org/t/uqlink-pass-paremeters-to-parser/799/1 "2020-08-11T06:58:25Z")

</div>

Hi

I do a couple of RBDO-Problems on the same construction pit, using an UQ link-model, linking ZSWalls with UQLab.

I am working on MATLAB R2020a and UQLab 1.3.0.

As the parsers for the different problems are quite similar, I would like to pass a parameter to the parser function. In my case the parameter would be a cell array of strings, telling the parser, which results to include in which order (e.g. {‘deviation’, ‘max. moment’}.

UQ standard models allow passing parameters, using the .Parameters option. Is there something similar for link models?

Does someone have a good idea of how to work around this problem?

---

<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:** [August 12, 2020, 8:15pm UTC](https://uqworld.org/t/uqlink-pass-paremeters-to-parser/799/4 "2020-08-12T20:15:01Z")

</div>

Hi @Colette_Jost ,

This is quite an interesting use case.

As far as I know, there is no field similar to `Parameters` of the default (i.e., standard) MODEL object in UQLink model, but I have some ideas for a workaround:

- The most straightforward thing to do is to create multiple parsers (yes, duplicate the files) each having a fixed value for the parameter. Granted, if you want to change the parameters on the fly or you have many combinations of results you want to include or not include, this is not ideal; or
- Modify the internal property of UQLink object, namely `myModel.Internal.Output.Parser` (the name `myModel` is your choice), _after_ the object is created. I’d like to recommend this approach because this is not intrusive, although there are some steps involved and a couple of MATLAB particularities you need to keep in mind.

Following up the second option, assuming your parser has the following signature:

```matlab
function Y = myParser(outputfile,params)

```

you can modify the `.Internal.Output.Parser` field of the MODEL object and assign to it a function handle as follows:

```matlab
myModel.Internal.Output.Parser = @(outputfile) myParser(outputfile,{'deviation','max. moment'});

```

Such a handle is required because internally, UQLink always calls the parser function with a single argument (i.e., the output file).

The above solution works as is for parameter given as a _literal_. Now if you put the parameter into a variable such as:

```matlab
params = {'deviation','max. moment'};
myModel.Internal.Output.Parser = @(outputfile) myParser(outputfile,params);

```

you need to define the parameter first before you create the function handle, just like the above. So for instance, if below is the first time you use `params`:

```matlab
myModel.Internal.Output.Parser = @(outputfile) myParser(outputfile,params);
params = {'deviation','max. moment'};
Y = uq_evalModel(myModel,X);

```

it will throw an error because `params` at the time the handle is created is not yet defined and it remains undefined when the handle is used later on by UQLink (from calling `uq_evalModel`) to parse the output file.

Also, every time you change the value of the parameter, you need to recreate the function handle and reassign it to the `.Internal.Output.Parser` field before calling `uq_evalModel`. Otherwise the new value will not be used.

Of course, you can wrap these steps into a single separate function that takes a parameter value and update the MODEL object so not to forget; you don’t need to explicitly return an updated MODEL object, because the object is passed _by-reference_.

I hope this helps (and let me know if it does, or doesn’t)! 😃

> **PS** : You cannot assign a function handle as a configuration option _before_ the object is created. That is, the following won’t work:
> 
> ```matlab
> ...
> ModelOpts.Output.Parser = @(outputfile) myParser(outputfile,{'deviation','max. moment'});
> ...
> myModel = uq_createModel(ModelOpts);
> 
> ```
> 
> Because apparently during the object initialization, the value to `ModelOpts.Output.Parser` is checked if it’s a char array; if not, an error is thrown. This is despite Table 3 of the [UQLink User Manual](https://www.uqlab.com/uqlink-user-manual) stating the option may accept a function handle 😅 (we’ll check it out!)

---

<div class="post-metadata">

**Author:** ![Colette\_Jost](https://uqworld.org/user_avatar/uqworld.org/colette_jost/32/590_2.png) [@Colette\_Jost](https://uqworld.org/u/Colette_Jost)\
**Post date:** [August 19, 2020, 7:41am UTC](https://uqworld.org/t/uqlink-pass-paremeters-to-parser/799/5 "2020-08-19T07:41:38Z")

</div>

Thanks for your idea! The approach using a function handle works like charm, with a little addition:

I created an empty file named dummyparser.m and set the

ModelOpts.Output.Parser = ‘dummyparser’;

Otherwise on model initiation I would get errors, such as:

The output file parser should be provided!  
The output parser name is incorrect or does not belong to the current MATLAB path

---

<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:** [August 19, 2020, 1:20pm UTC](https://uqworld.org/t/uqlink-pass-paremeters-to-parser/799/6 "2020-08-19T13:20:59Z")

</div>

Great to hear that! 😄

Yes, we are aware of that issue (see my _PS_ above 😃). The point is to specify as the parser before the object is created a filename (a char array) that exists in the current path and _not_ a function handle.

Because you are going to anyway change the parser after the object is created, this parser file can indeed be a dummy file, just like you did.

---

<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:** [August 25, 2020, 11:56am UTC](https://uqworld.org/t/uqlink-pass-paremeters-to-parser/799/7 "2020-08-25T11:56:38Z")

</div>


