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!