Reputation: 2809
I want to compile a MEX from a function that has the following code (MATLAB R2015a
):
function r = MyFunc(x,type)
ind = randi(numel(x), 1);
getInd = @getIndFixed;
if strcmpi(type, 'random')
ind = numel(x);
getInd = @getIndRandom; % error here
end
x(getInd(ind)) = 1;
end
function k = getIndFixed(n)
k = n;
end
function k = getIndRandom(n)
k = randi(n, 1);
end
I get the type mismatch
error between getIndFixed
and getIndRandom
at the line specified above:
Type mismatch: function_handle getIndFixed ~= getIndRandom.
In C, the signature of the function would be:
int (*getInd)(int);
int getIndFixed(int);
int getIndRandom(int);
//...
getInd = getIndFixed;
getInd = getIndRandom;
Upvotes: 0
Views: 303
Reputation: 4477
You cannot change function handles to different functions after they are assigned in code generation. Unlike "C" these function calls are resolved at compile time. If your type
input is constant then you can write your code as
function r = MyFunc(x,type)
if strcmpi(type, 'random')
ind = numel(x);
getInd = @getIndRandom; % error here
else
ind = randi(numel(x), 1);
getInd = @getIndFixed;
end
x(getInd(ind)) = 1;
end
If type
is not constant you need to move everything inside if or else branch.
Upvotes: 0