swig icon indicating copy to clipboard operation
swig copied to clipboard

Implement cast

Open jaeandersson opened this issue 9 years ago • 3 comments

http://es.mathworks.com/help/matlab/ref/cast.html

jaeandersson avatar Sep 10 '15 21:09 jaeandersson

Probably a tricky thing to implement...

jaeandersson avatar Sep 10 '15 21:09 jaeandersson

Yes, I agree that casting is tricky on the Matlab side. But it can be supported on the c++ side using cast operators. Here is an example from our source for double():

%rename("double") class::operator const double;

Then in Matlab you can simply call double(class). You could also add additional conversion operators for other primitive data types if desired.

dcb210 avatar Sep 10 '15 21:09 dcb210

double(A) is supposed to mean "convert to double storage", not "convert to a double scalar", so %rename("double") class::operator double is not necessarily the answer. Anyway, in many cases, %rename("double") will probably do the trick. In CasADi, we use full(A) and sparse(A) to convert CasADi matrices to dense and sparse MATLAB matrices respectively.

jaeandersson avatar Sep 11 '15 09:09 jaeandersson