Passing a pointer from Matlab to C
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 28 Mar 2017
You did not indicate what difficulty you encountered?
Walter Roberson
le 28 Mar 2017
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
le 28 Mar 2017
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
le 30 Mar 2017
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur MATLAB Compiler SDK dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!