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

3 ビュー (過去 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
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 Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by