Passing a pointer from Matlab to C
显示 更早的评论
I am attempting to pass a matrix from a class in Matlab to a code in C. The code in C cannot be changed and requires a double* to be passed in as an argument. I am trying to use libpointer(...) to modify the matrix into something that can be passed to the C code through a calllib(...) function.
I cannot post all of the code because it is quite extensive. The function of the Matlab code reads:
function obj = set.RP(obj, vals)
rho = vals{1};
pres = vals{2};
if strcmp(rho,'None')
rho = obj.R;
end
if strcmp(pres, 'None')
pres = obj.P;
end
disp(rho)
disp(pres)
idxptr = libpointer('doublePtr',[rho, pres]);
disp(idxptr)
calllib('canteraLib', 'thermo_set_RP', obj.thermo, idxptr)
end
The function in the C code reads:
int thermo_set_RP(int n, double* vals)
{
try{
ThermoCabinet::item(n).setState_RP(vals[0], vals[1]);
return 0;
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}
Thank you for your time
-Emil
5 个评论
Walter Roberson
2017-3-28
You did not indicate what difficulty you encountered?
Walter Roberson
2017-3-28
Your use of strcmp() hints that rho and pres might not be double . If the intent is that they can be numeric or the string 'None' then I would have expected you would use a test like
if ischar(rho) && strcmp(rho, 'None')
Emil A Atz
2017-3-28
I doubt it matters, but the sample C code is not C, but C++. C does not have try catch or namespaces.
Is the C++ function defined as extern "C" in its header file?
"the code fails". And that means ...? access violation? BSOD? wrong value returned? an error is thrown by matlab?
Emil A Atz
2017-3-30
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB Compiler SDK 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!