Convert function with struct input to mex while number of fields is unknown

조회 수: 1 (최근 30일)
Marc Laub
Marc Laub 2020년 12월 3일
댓글: Marc Laub 2020년 12월 3일
Hey guys,
I am trying to translate a matlab function of mine into a mex function but I am facing some issues.
The problem is, that the input into my function is a struct with sub-structs, each containing the same amount of fields, but an unknown amount of fields.
Inside the function I check that with:
if isfield(struct.substruct,'example')==1
...
else
...
end
For MatLab that is no problem, but for mex it is a problem, since I have to declare the number of fields while generating it and there is no possibility to do this like with arrays where you can say "up to ..."
So if the field does not exist, the number of fields is unepexted for mex and it doesnt run.
Is there any workaround for this??
Just bypassing the struct and simply giving the field values as inputs for my function doenst work either, since the field and its values do not exist sometimes.
The only way would be to write the funtction multiple times and check the number of fields before and then call with an if statement the function with the right number of input arguments. But for me it seems unlikely that this is the best way to solve my problem.
Best regards
  댓글 수: 4
James Tursa
James Tursa 2020년 12월 3일
Do you have to use the Coder for this particular routine or will a hand-written mex routing work? Or maybe you can split your function into an interface routine that calls a work routine and hand-code only the interface routine. How complicated is the routine?
Marc Laub
Marc Laub 2020년 12월 3일
You dont need to use the coder, but I need. Since I cant write C and I wanted to speed up by code by writing the bottleneck function as a mex.
Its a function inside a smulation, that it called thousend of multiple times. Acually this isnt even the bottle neck function, its just one function that is also called with the bottleneck, but I wanted to do the whole function both are nested in as mex, just to make it "complete".
The routine isnt complicated. I just have to calculate some values, some of the inputs come from this struct, but because the routine is universal, and sometimes the values cant be calculated because the simulation parameters changed, I simply check by the isfield if the value can be calculated at all.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by