Hello, this is just a question for understanding :-)
I have a c++ mex function that takes in a real scalar. In the function code I have the assignment
double x=inputs[0][0];
This does the right conversion even when the MATLAB input type is not double, e.g. single or uint8.
As I understand it, the way this works is that input[0] is a TypedArray<T>. Depending on T, the assignment double x=input[0][0]; figures out at compile time how the bits in the input are converted to the bits of a double. And the internal c++ code of the MATLAB c++ api would need to look like
"If the MATLAB input is of type uint8, create a TypedArray<uint8_t>.
if the MATLAB input type is single, create TypedArray<float>.
etc."
But templates are a compile time construct, so how is that even possible?

댓글 수: 2

James Tursa
James Tursa 2022년 8월 14일
Please post a minimum working example of code that runs and then we can comment on how it works or doesn't work.
I'm sorry for answering so late. Here is a minimum working example: The file "mySquare.cpp" contains
#include "mex.hpp"
#include "mexAdapter.hpp"
using matlab::mex::ArgumentList;
using namespace matlab::data;
class MexFunction : public matlab::mex::Function {
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
double x = inputs[0][0];
ArrayFactory factory;
TypedArray<double> t = factory.createScalar(x * x);
outputs[0] = t;
}
};
which I compiled in Matlab with
mex mySquare.cpp
(with MinGW 6.3.0 on Win10 64bit), creating "mySquare.mexw64". Then, both
mySquare(3)
and e.g.
mySquare(uint8(3))
return the correct result double(9).

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB Data API for C++에 대해 자세히 알아보기

제품

질문:

2022년 8월 11일

편집:

2022년 10월 27일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by