# Call Matlab inbuilt function from Julia

**URL:** https://discourse.julialang.org/t/call-matlab-inbuilt-function-from-julia/76845
**Category:** General Usage
**Tags:** question
**Created:** [February 21, 2022, 3:05pm UTC](https://discourse.julialang.org/t/call-matlab-inbuilt-function-from-julia/76845 "2022-02-21T15:05:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [February 21, 2022, 3:05pm UTC](https://discourse.julialang.org/t/call-matlab-inbuilt-function-from-julia/76845/1 "2022-02-21T15:05:05Z")

</div>

Hi, there is a problem when I try to call the bayesopt function (inbuilt function) from Julia, here is a simple example, and it will give an error like follows.

```julia
Unable to use a value of type table as an index.

Error in @(tbl)matlab_jl_2(tbl)

Error in BayesianOptimization/callObjNormally (line 2576)
                        Objective = this.ObjectiveFcn(conditionalizeX(this, X));

Error in BayesianOptimization/callObjFcn (line 481)
                        = callObjNormally(this, X);

Error in BayesianOptimization/runSerial (line 1996)
                    ObjectiveFcnObjectiveEvaluationTime, ObjectiveNargout] = callObjFcn(this, this.XNext);

Error in BayesianOptimization/run (line 1948)
                this = runSerial(this);

Error in BayesianOptimization (line 457)
            this = run(this);

Error in bayesopt (line 323)
Results = BayesianOptimization(Options);

```

As I know, within matlab, the variables are transfered to `mdlfun()` in type of table, here I just want to run the objective function in Julia (cause it’s fast), and use matlab `bayesopt` function (cause it’s convenient), thanks in advance for any suggestions

```julia
using MATLAB

function f(x, y)
    return -x - y
end

function mdlfun(tbl)
    x = tbl.v1
    y = tbl.v2
    return f(x, y)
end

mat"""
xrange = optimizableVariable('v1',[-2,2],'Type','real');
yrange = optimizableVariable('v2',[-5,5],'Type','real');
var=[xrange yrange];

bayesObject =bayesopt(@(tbl)$mdlfun(tbl),var,...
    'MaxObjectiveEvaluations',50)

"""

```

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [February 21, 2022, 3:32pm UTC](https://discourse.julialang.org/t/call-matlab-inbuilt-function-from-julia/76845/2 "2022-02-21T15:32:30Z")

</div>

Can’t help you, but I just want to comment that your statement

> [@xspeng](#):
>
> here I just want to run the objective function in Julia (cause it’s fast), and use matlab `bayesopt` function (cause it’s convenient), thanks in advance for any suggestions

does not really make sense in this context (if I’m understanding correctly). Everything inside `mat""" ... """` will be converted to Matlab code, run in Matlab, and at Matlab speed. It isn’t related to Julia at all at that point.

---

<div class="post-metadata">

### Author: ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)
#### Post date: [February 21, 2022, 3:44pm UTC](https://discourse.julialang.org/t/call-matlab-inbuilt-function-from-julia/76845/3 "2022-02-21T15:44:08Z")

</div>

Yeah, you are correct, I also have this worry, but I find that I can import python packages and let the objective run within julia, and use python bayesian optimization function to optimize it, here is another example, I want to change to matlab because I find bayesopt is more efficient to find the optimal result, at least from my experience. So, I just hope to interpolate with matlab~

```julia
using PyCall

function black_box_function(; x, y)
    return -x - y
end

py"
pbounds = {'x': (-2, 2), 'y': (-5, 5)}
"
temp = pyimport("bayes_opt")
optimizer = temp.BayesianOptimization(
    f = black_box_function,
    pbounds = py"pbounds",
    random_state = 7, # random seed
)
optimizer.maximize(
    init_points = 2,
    n_iter = 5
)

```
