run a matlab function from Julia - matlab

I am trying to use a custom matlab function in julia but I am getting an error.
I tried to do the following (https://github.com/JuliaInterop/MATLAB.jl)
mat"""
function [result] = run(x_pos, y_pos)
result = x_pos + y_pos;
end
"""
but I am getting this error:
Error: Function definition not supported in this context. Create functions in code file.
Unrecognized function or variable 'x_pos'.
end;
|
Error: Illegal use of reserved keyword "end".
How can I use a custom matlab function in julia?

Related

How to solve the error undefined the function or variables in matlab

This is the code I write in Matlab. As you can see, I have defined the symbolic variable tao. However, it still shows that undefined variable or functions. I didn't know where is wrong in the code.
The function is like this:
Ch = 0.8; H=0.6088;
syms t u s k tao
fsb= limit( symsum( (int( Ch*s^(1/2)*int((u-s)^(H-3/2)*u^(H-1/2),u,floor(t/tao)*tao,floor((t+tao)/tao)*tao),s,(k-1)*tao,k*tao ))^2,k,1,floor(t/tao)),tao,0) + ...
+limit( (int( Ch*s^(1/2)*int((u-s)^(H-3/2)*u^(H-1/2),u,s,floor((t+tao)/tao)*tao ),s,floor(t/tao)*tao,floor((t+tao)/tao)*tao) )^2,tao , 0);
fsb = matlabFunction(fsb);
sss=fsb(1);
I have tried change the path into the Matlab installed path, but it didn't work.
The fsb is a function defined by my self. It's a complicated function including integral and limit.
This is the information about the error:
undefined functions or variables 'tao'
symengine>#(t)limit(integral(#(s)sqrt(s).*integral(#(u)u.^(6.8e1./6.25e2).*1.0./(-s+u).^(5.57e2./6.25e2),s,tao.*floor((t+tao)./tao)).*(4.0./5.0),tao.*floor(t./tao),tao.*floor((t+tao)./tao)).^2,tao==0.0)+limit(symsum(integral(#(s)sqrt(s).*integral(#(u)u.^(6.8e1./6.25e2).*1.0./(-s+u).^(5.57e2./6.25e2),tao.*floor(t./tao),tao.*floor((t+tao)./tao)).*(4.0./5.0),tao.*(k-1.0),k.*tao).^2,k,1.0,floor(t./tao)),tao==0.0,Real)

Not enough input arguments when calling GA toolbox

I called Matlab's genetic algorithm solver, ga, in the following way:
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
where theta is a 26-by-1 column vector that needs to be optimized.
So in the main function, it goes like this:
clc
clear
global var1 var2...
load ('abcd.mat')
theta0=[1 2 3....];
LB=[26-by-1 row vector];
UB=[26-by-1 row vector];
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
The fitness function, smmobj, is defined as:
function [obj]=smmobj(theta)
global var1 var2...
But when I run it, it always says:
Error using smmobj (line 4)
Not enough input arguments.
Error in SMMga (line 32)
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
But I run the fitness function by itself, it works.
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
will throw that error because you are essentially calling the function smmobj (with no parameters) instead of passing your ga function a function handle to your objective function. You do this using the # symbol so
[theta,fval,exitflag] = ga(#smmobj,26,[],[],[],[],LB,UB,[],[]);
Incidentally, I would recommend that you do not use global in order to pass the extra parameters of var1 and var2 to your objective function but rather use anonymous functions:
[theta,fval,exitflag] = ga(#(x)(smmobj(x,var1,var2)),26,[],[],[],[],LB,UB,[],[]);
and then change smmobj's definition to
function [obj]=smmobj(theta,var1,var2)

an error received while executing my function?

When I execute the following:
function [ x ] = addya( varargin )
x=varargin{1};
t=varargin{1};
if(nargin>1)
for i=2:nargin
t=t+varargin(i);
end;
end
x=t;
The error i am getting is:
addya(1,1) ??? Undefined function or method
'addya' for input arguments of type 'double'.
please suggest changes and errors.
Make sure this function is saved in a file called addya.m.
Moreover, as mentioned by il_raffa there's a typo - inside the loop: you should access varargin using {}.
The following code works for me when saved as addya.m:
function [ x ] = addya( varargin )
x=varargin{1}; %// Why is this needed?
t=varargin{1};
if(nargin>1)
for i=2:nargin
t=t+varargin{i};
end;
end
x=t;
Also, I would suggest to refrain from using i as a loop index due to possible complication with complex numbers.

??? Undefined function or variable

Why do i get the error
??? Undefined function or variable
when trying to call the following function:
function Diff= myfun3(wk,omega)
wcalc=inv(lambda* Passetcovar)*inv(inv(tau * Passetcovar)+ PMat(i,:)'*inv(omega)*PMat(i,:))*(inv(tau * Passetcovar)*Pi+ PMat(i,:)'*inv(omega)*Q(i,:));
Diff=sum((wk-wcalc).^2);
end
All the parameters in the function are defined and correct.
I am calling this function using myfun3(wk,omega) but I get error messages such as
??? Undefined function or variable 'lambda'.
Even when lambda has been defined eslewhere.
lambda isn't defined in the function workspace, even if it is defined in your base workspace. Either pass it as an input to the function or define it as a global variable. See http://www.mathworks.co.uk/help/matlab/matlab_prog/share-data-between-workspaces.html for more details.

Anonymous function inside a script error

I created an anonymous function inside a script and I can't get MATLAB to run the fminsearch? Here's an what I have so far:
V=x(1);
f=x(2);
q=#(x) (pi.*D.*L)./(1000.*V.*f);
fminsearch(#q,x);
The variables D and L are defined, but MATLAB gives me the following error:
Error: File: Testing.m Line: 51 Column: 17
"q" was previously used as a variable, conflicting with its use here as the name of a function or command.
See "How MATLAB Recognizes Command Syntax" in the MATLAB documentation for details.
q is not mentioned before this command. What am I doing wrong?
Another thing that could solve my problem is to get my script to write a function file, but how to do that?
Remove the second #:
V=x(1);
f=x(2);
q=#(x) (pi.*D.*L)./(1000.*V.*f);
fminsearch(q,x);
q is a function handle. fminsearch expects a function handle. You can create a function handle out of a function using an # (e.g. #min), but you don't need to do that here.
You can also write the anonymous function inline with the search command:
V=x(1);
f=x(2);
fminsearch(#(x) (pi.*D.*L)./(1000.*V.*f),x);
UPDATE (credits to #wakjah)
For your code to do anything sensible, you should use the argument x of the anonymous function:
x0 = [initialV, initialF];
fminsearch(#(x) (pi.*D.*L)./(1000.*x(1).*x(2)), x0);
#function creates a function handle for an existing function.
q = #(x) whatever... creates a function handle called q.
But, you can't create a function handle for a function handle, only for a function.
See this:
>> fones = #ones
fones =
#ones
>> ffones = #fones
Error: "fones" was previously used as a
variable,
conflicting with its use here as the name
of a function or command.
See MATLAB Programming, "How MATLAB
Recognizes Function Calls That Use
Command Syntax" for details.
In Matlab, a function handle is a kind of a pointer to a function and is distinct from a function (unlike in some other languages where the a function identifier can be passed and stored as any other variable).
It's important to note that calling a function and a function handle results in the same behaviour. Except for the case where the identifier is used without any parentheses following it:
>> ones
ans =
1
>> fones
fones =
#ones
>> ones(2)
ans =
1 1
1 1
>> fones(2)
ans =
1 1
1 1